CodeForces 359A Table
2017-08-29 21:05
1981 查看
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the
table from top to bottom starting from one and the columns — from left to right starting from one.
We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table
corners are cells: (1, 1), (n, 1), (1, m), (n, m). Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move,
he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of
the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).
Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
Input:
The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).
Next n lines contain the description of the table cells. Specifically, the i-th
line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then
cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one
cell is good. It is guaranteed that no good cell is a corner.
Output:
Print a single number — the minimum number of operations Simon needs to carry out his idea.
Example:Input
3 3 0 0 0 0 1 0 0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note:
In the first sample, the sequence of operations can be like this:
For the first time you need to choose cell (2, 2) and corner (1, 1).
For the second time you need to choose cell (2, 2) and corner (3, 3).
For the third time you need to choose cell (2, 2) and corner (3, 1).
For the fourth time you need to choose cell (2, 2) and corner (1, 3).
In the second sample the sequence of operations can be like this:
For the first time you need to choose cell (3, 1) and corner (4, 3). For the second time you need to choose cell (2, 3) and corner (1, 1).
思路:
[b]因为四个角不会成为good
所以good点可以分为两类:[/b]
[b]1:在边上的点。(非角)
2:不在边上的点(非角)[/b]
[b]而在边上的点只需两次就能涂满全部格,不在边上的需要四次,即答案只有4和2两种情况。[/b]
[b][b]代码:[/b][/b]
#include <cstdio>
#include <iostream>
using namespace std;
int n,m;
int key;
int main(){
scanf("%d %d",&n,&m);
for(int i=1 ; i<=n ; i++){
for(int j=1 ; j<=m ; j++){
int mid;
scanf("%d",&mid);
if(mid == 1){
if(i == 1 || j == 1 || i == n || j == m)key = 1;
}
}
}
if(key)printf("2");
else printf("4");
return 0;
}
相关文章推荐
- CodeForces 359A - Table(思维)
- Codeforces —— 359A Table
- codeforces 359A- Table(简单思维)
- codeforces 359A- Table(简单思维)
- Codeforces_359A_Table
- Codeforces 22B Bargaining Table【暴力+思维】
- Codeforces 140 A. New Year Table(计算几何)
- CodeForces 71 C.Round Table Knights(数论+dp)
- CodeForces 266A Stones on the Table
- codeforces 418 C Square Table (随机算法)
- C. Table Decorations(Codeforces)(规律)
- CodeForces 581C - GCD Table(思维)
- codeforces 22B B. Bargaining Table(dp)
- [Codeforces] 361A - Levko and Table
- 【codeforces 765C】Table Tennis Game 2
- Codeforces 253D-Table with Letters - 2
- 【codeforces 509A】Maximum in Table
- 【Codeforces】338 D GCD Table 中国剩余定理(孙子定理)
- codeforces 425B Sereja and Table(状态压缩,也可以数组模拟)
- Codeforces 338D GCD Table 中国剩余定理