传送门:Red and Black
Red and Black
Source : Japan Domestic 2004 | |||
Time limit : 1 sec | Memory limit : 32 M |
Submitted : 834, Accepted : 550
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.
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)
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 0Sample Output
45 59 6 13
解题报告:
此题为典型的搜索题,搜索题有深度优先(DFS)和广度优先(BFS)两种。
DFS递归。很显然,当找到起始状态后,直接向四个方向递归即可。思路很简单代码如下:
#include<stdio.h>
#include<string.h>
char s[30][30];
int maps[30][30],h,w,sum;
void dfs(int i,int j){
sum++;
maps[i][j]=1;
if(i+1<w&&maps[i+1][j]==0&&s[i+1][j]=='.')
dfs(i+1,j);
if(j+1<h&&maps[i][j+1]==0&&s[i][j+1]=='.')
dfs(i,j+1);
if(i-1>=0&&maps[i-1][j]==0&&s[i-1][j]=='.')
dfs(i-1,j);
if(j-1>=0&&maps[i][j-1]==0&&s[i][j-1]=='.')
dfs(i,j-1);
}
int main(){
int i,j;
while(scanf("%d%d",&h,&w)==2&&(h||w)){
memset(maps,0,sizeof(maps));
sum=0;
for(i=0;i<w;i++)
scanf("%s",s[i]);
for(i=0;i<w;i++)
for(j=0;j<h;j++)
if(s[i][j]=='@')
dfs(i,j);
printf("%d\n",sum);
}
return 0;
}
DFS非递归
因为DFS可以使用栈(stack)实现。所以实现起来就简单了。
#include<iostream>
#include<stack>
#include<cstdio>
using namespace std;
struct node{
int x,y;
}st;
char s[30][30];
int maps[30][30];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int dfs(node st,int h,int w){
stack<node> q;
int i,cnt=0;
node a,b;
while(!q.empty()){
q.pop();
}
q.push(st);
maps[st.x][st.y]=1;
cnt++;
while(!q.empty()){
a=q.top();
q.pop();
for(i=0;i<4;i++){
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
if(b.x<0||b.y<0||b.x>w-1||b.y>h-1)
continue;
if(maps[b.x][b.y]==0){
q.push(b);
maps[b.x][b.y]=1;
cnt++;
}
}
}
return cnt;
}
int main(){
int h,w,i,j;
while(scanf("%d%d",&h,&w)==2&&(h||w)){
for(i=0;i<w;i++)
scanf("%s",s[i]);
for(i=0;i<w;i++)
for(j=0;j<h;j++){
if(s[i][j]=='.')
maps[i][j]=0;
else if(s[i][j]=='#')
maps[i][j]=1;
else{
maps[i][j]==0;
st.x=i;
st.y=j;
}
}
printf("%d\n",dfs(st,h,w));
}
return 0;
}
BFS
因为BFS可以使用队列(queue)实现。所以代码与DFS非递归相似。
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
struct node{
int x,y;
}st;
char s[30][30];
int maps[30][30];
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int bfs(node st,int h,int w){
queue<node> q;
int i,cnt=0;
node a,b;
while(!q.empty()){
q.pop();
}
q.push(st);
maps[st.x][st.y]=1;
cnt++;
while(!q.empty()){
a=q.front();
q.pop();
for(i=0;i<4;i++){
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
if(b.x<0||b.y<0||b.x>w-1||b.y>h-1)
continue;
if(maps[b.x][b.y]==0){
q.push(b);
maps[b.x][b.y]=1;
cnt++;
}
}
}
return cnt;
}
int main(){
int h,w,i,j;
while(scanf("%d%d",&h,&w)==2&&(h||w)){
for(i=0;i<w;i++)
scanf("%s",s[i]);
for(i=0;i<w;i++)
for(j=0;j<h;j++){
if(s[i][j]=='.')
maps[i][j]=0;
else if(s[i][j]=='#')
maps[i][j]=1;
else{
maps[i][j]==0;
st.x=i;
st.y=j;
}
}
printf("%d\n",bfs(st,h,w));
}
return 0;
}