Red and Black
Time Limit: 1000MSMemory Limit: 30000KTotal
Submissions: 23001Accepted: 12407
问题
//1.遍历方式不对,连被红块堵在角落里的黑块都给遍历到了
1.二维数组长宽分不清
2.把ans++;room[nx][ny];放在控制范围内if语句里
3.填坑的时候有问题,room[x][y]应该填的是nx,ny
4.
while
(~scanf(
"%d%d"
,&w,&h),w+h)这个可以排除掉数组为0的情况还要加上#include<cstdio>
5.人家是要循环着交题,不能用if,
6.ans没有被更新,全局变量
#include <iostream>
#include <cstdio>
#define MAX_W 20
#define MAX_H 20
using namespace std;
int W, H;
char room[MAX_H][MAX_W + 1];
int ans;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void print()
{
for(int i = 0; i < H; i++)
{
for(int j = 0; j < W; j++)
{
cout << room[i][j];
}
cout << endl << endl;
}
}
void dfs(int x, int y)
{
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if(0 <= nx && nx < H && 0 <= ny && ny < W && room[nx][ny] == '.')
{
room[nx][ny] = '#';
//print();
ans ++;
dfs(nx, ny);
}
}
return;
}
void solve()
{
for(int i = 0; i < H; i++)
for(int j = 0; j < W; j++)
{
if(room[i][j] == '@')
dfs(i, j);
}
cout << ans << endl;
}
int main()
{
while(~scanf("%d%d",&W,&H),W + H)
{
for(int i = 0; i < H; i++)
for(int j = 0; j < W; j++)
{
cin >> room[i][j];
}
ans = 1;
solve();
}
return 0;
}