Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2101 Accepted Submission(s): 1370
Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
Source
Recommend
Eddy
题目大意:@是一个man,#是红瓷砖,是不能进去的,.是可以走的地方,输入地图,输出man可以走的格子数。
解题思路:种子填充法,利用队列。先将man入队。扫描四周是否可走,可走则入队,出队中心点。依次出队入队。直到队空。
总结:注意坐标的统一。初始化flag时,当是二维的时候不能用mamset(flag,0,sizeof(int))。
AC代码:
#include<iostream>
#include <queue>
using namespace std;
char map[21][21];
int di[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct
{
int x,y;
}point;
point man;
int n,m;
void tiles()
{
int flag[21][21]={0};
int i,c=0;
queue<point> q;
point t,t1;
q.push(man);
flag[man.y][man.x]=1;
c++;
while(!q.empty())
{
t=q.front();
q.pop();
for(i=0;i<4;i++)
{
t1.x=t.x+di[i][0];
t1.y=t.y+di[i][1];
if(map[t1.y][t1.x]!='#'&&flag[t1.y][t1.x]==0&&(t1.x<n&&t1.x>=0)&&(t1.y<m&&t1.y>=0))
{
q.push(t1);
flag[t1.y][t1.x]=1;
c++;
}
}
}
printf("%d/n",c);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m))
{
getchar();
if(n==0&&m==0) break;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
{
man.x=j;man.y=i;
}
}
getchar();
}
tiles();
}
return 0;
}
#include <queue>
using namespace std;
char map[21][21];
int di[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct
{
int x,y;
}point;
point man;
int n,m;
void tiles()
{
int flag[21][21]={0};
int i,c=0;
queue<point> q;
point t,t1;
q.push(man);
flag[man.y][man.x]=1;
c++;
while(!q.empty())
{
t=q.front();
q.pop();
for(i=0;i<4;i++)
{
t1.x=t.x+di[i][0];
t1.y=t.y+di[i][1];
if(map[t1.y][t1.x]!='#'&&flag[t1.y][t1.x]==0&&(t1.x<n&&t1.x>=0)&&(t1.y<m&&t1.y>=0))
{
q.push(t1);
flag[t1.y][t1.x]=1;
c++;
}
}
}
printf("%d/n",c);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m))
{
getchar();
if(n==0&&m==0) break;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
{
man.x=j;man.y=i;
}
}
getchar();
}
tiles();
}
return 0;
}