炸弹版扫雷, 注意不要越界。
Accepted | 2922 | C++ | 00:00.73 | 4356K |
#include<stdio.h>
int room[1000][1000 ];
void expand(int row, int col)
{
int k = room[row][col];
room[row][col] = 0 ;
for(int i = col - 1; i >= col - k && i >= 0; i-- )
if(room[row][i] > 0 ) expand(row, i);
for(int i = row - 1; i >= 0; i-- )
if(room[i][col] > 0 ) expand(i, col);
}
int solve(int n, int m)
{
int res = 0 ;
for(int i = 0; i < n; i++ )
for(int j = 0; j < m; j++ )
scanf("%d", & room[i][j]);
for(int i = n - 1; i >= 0; i-- )
for(int j = m - 1; j >= 0; j-- )
if(room[i][j] > 0 )
expand(i, j), res++ ;
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2922.txt", "r" , stdin);
#endif
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
printf("%d/n" , solve(n, m));
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}
int room[1000][1000 ];
void expand(int row, int col)
{
int k = room[row][col];
room[row][col] = 0 ;
for(int i = col - 1; i >= col - k && i >= 0; i-- )
if(room[row][i] > 0 ) expand(row, i);
for(int i = row - 1; i >= 0; i-- )
if(room[i][col] > 0 ) expand(i, col);
}
int solve(int n, int m)
{
int res = 0 ;
for(int i = 0; i < n; i++ )
for(int j = 0; j < m; j++ )
scanf("%d", & room[i][j]);
for(int i = n - 1; i >= 0; i-- )
for(int j = m - 1; j >= 0; j-- )
if(room[i][j] > 0 )
expand(i, j), res++ ;
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("2922.txt", "r" , stdin);
#endif
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
printf("%d/n" , solve(n, m));
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
}