HDU - 1312 【DFS】(別踩紅塊兒~)
覺得自己把深搜忘乾淨了,遞迴著遞迴著就會了呢~【雖然改了賊久。。】
最簡單深搜~
題目描述:
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 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#[email protected]#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### [email protected] ###.### ..#.#.. ..#.#.. 0 0Sample Output
45 59 6 13
題目大意:
給定一張地圖,上面 “ . ” 代表可以通行,“ # ”代表障礙物,不可通過;給定起點,問可到達多少點?
解題思路:
深搜呀~,四個方向遍歷呀~
【無數的小錯誤讓我感到頭禿,,】
程式碼實現:
#include<iostream>
#include<cstring>
using namespace std;
int x,
y,
ans=0,
mapp[21][21],
biao[21][21],
d[2][4]={{-1,+1, 0, 0},{ 0, 0,-1,+1}};
//DFS
void dfs(int sx,int sy){
if(sx<=0||sx>y||sy<=0||sy>x||mapp[sx][sy]==1||biao[sx][sy]==1){
return ;
}
if(biao[sx][sy]==0){
ans++;
biao[sx][sy]=1;
}
for(int i=0;i<=3;i++){
dfs(sx+d[0][i],sy+d[1][i]);
}
}
int main(){
while(1){
cin>>x>>y;
if(x==0&&y==0){
return 0;
}
//初始化
ans=0;
memset(mapp,0,sizeof(mapp));
memset(biao,0,sizeof(biao));
//輸入
int stx,sty;
for(int i=1;i<=y;i++){
char ch;
for(int j=1;j<=x;j++){
cin>>ch;
if(ch=='.'){
}else if(ch=='#'){
mapp[i][j]=1;
biao[i][j]=1;
}else if(ch=='@'){
stx=i;
sty=j;
}
}
}
dfs(stx,sty);
cout<<ans<<endl;
}
}
PS:如果你想看程式碼內部實現過程,come here~【一般有這樣醜醜的程式碼出現,就說明,debug的路程比較艱辛】
#include<iostream>
#include<cstring>
using namespace std;
int x,
y,
ans=0,
mapp[21][21],
biao[21][21],
d[2][4]={{-1,+1, 0, 0},{ 0, 0,-1,+1}};
void dfs(int sx,int sy){
cout<<"\n sx="<<sx<<" sy="<<sy;
if(sx<=0||sx>y||sy<=0||sy>x||mapp[sx][sy]==1||biao[sx][sy]==1){
cout<<"不可以"<<endl;
return ;
}
if(biao[sx][sy]==0){
ans++;
biao[sx][sy]=1;
cout<<"可以 "<<endl;
}
cout<<"\n biao="<<endl;
for(int i=1;i<=y;i++){
cout<<" ";
for(int j=1;j<=x;j++){
cout<<biao[i][j];
}cout<<endl;
}cout<<" ans="<<ans<<endl;
for(int i=0;i<=3;i++){
dfs(sx+d[0][i],sy+d[1][i]);
}
}
int main(){
while(1){
cin>>x>>y;
if(x==0&&y==0){
return 0;
}
int stx,
sty;
ans=0;
memset(mapp,0,sizeof(mapp));
memset(biao,0,sizeof(biao));
for(int i=1;i<=y;i++){
char ch;
for(int j=1;j<=x;j++){
cin>>ch;
if(ch=='.'){
}else if(ch=='#'){
mapp[i][j]=1;
biao[i][j]=1;
}else if(ch=='@'){
stx=i;
sty=j;
}
}
}
cout<<"\nmap="<<endl;
for(int i=1;i<=y;i++){
for(int j=1;j<=x;j++){
cout<<mapp[i][j];
}cout<<endl;
}
cout<<"\nbiao="<<endl;
for(int i=1;i<=y;i++){
for(int j=1;j<=x;j++){
cout<<biao[i][j];
}cout<<endl;
}
dfs(stx,sty);
cout<<ans<<endl;
}
}