先贴一下题目
**Red and Black **
Poj1979地址
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)
The end of the input is indicated by a line consisting of two zeros.
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
这题的意思是".“代表黑色的砖块,就是可以走的地方,”#"则是红色的砖块,就是不能走的地方,@是起点,题目让统计一下最多能走多少个砖块
这题上来一想就觉得要用dfs 毕竟暴力出奇迹 再加上队里今天dhz(手搓线段树的大佬)讲了一下,我就把答案整理了一下 一种是dfs 一种是bfs 由于现在实在是太晚了(一点二十二) 明天还有高数课 我就先贴一下代码把23333贴完就溜了全都是手搓的 ==
#include<bits/stdc++.h>// dfs的版本
using namespace std;
int ans;
int a[30][30];
bool vis[30][30];
int dx[]={1,-1,0,0};
int dy[]={0,0,-1,1};
int n,m;
int sx,sy;
void dfs(int x,int y) {
for(int i=0;i<4;i++) {
int xx=x+dx[i];
int yy=y+dy[i];
if(!vis[xx][yy]&&xx>=0&&xx<m&&yy>=0&&yy<n&&!a[xx][yy]) {
vis[xx][yy]=1;
ans++;
dfs(xx,yy);
}
}
}
int main() {
while(cin>>n>>m) {
if(m==0&&n==0)
break;
ans=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) {
char c;
cin>>c;
if(c=='.')
a[i][j]=0;
else if(c=='#')
a[i][j]=1;
else if(c=='@') {
a[i][j]=1;
sx=i;
sy=j;
}
}
dfs(sx,sy);
cout<<ans+1<<endl;
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
}
}
下面是bfs的版本 突然感觉bfs 也有点感觉了呢!!但是没办法我还是一条咸鱼,只能等待咸鱼飞天 哦不,是翻身的那一天来到hhhh
#include<iostream>
#include<queue>
using namespace std;
int m,n;
int sx;
int sy;
int cnt=0;
struct node {
int x;
int y;
};
bool a[30][30];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void bfs(int x,int y) {
queue<node>q;
node b;
b.x=x;
b.y=y;
q.push(b);
while(!q.empty()) {
node c;
c=q.front();
q.pop();
for(int i=0;i<4;i++) {
node d;
d.x=c.x+dx[i];
d.y=c.y+dy[i];
if(d.x>=0&&d.y>=0&&d.x<m&&d.y<n&&!a[d.x][d.y]) {
q.push(d);
a[d.x][d.y]=1;
cnt++;
}
}
}
}
int main() {
while(cin>>n>>m) {
if(m==0&&n==0)
break;
cnt=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++) {
char c;
cin>>c;
if(c=='.')
a[i][j]=0;
else if(c=='#')
a[i][j]=1;
else if(c=='@') {
a[i][j]=1;
sx=i;
sy=j;
}
}
bfs(sx,sy);
cout<<cnt+1<<endl;
}
}
这题看起来是挺长的,但是对于一道搜索题来说感觉就是个板子啊,就是一道裸体 困死了我去
溜了溜了 明天和dfs bfs刚到底!!!!