B - Red and Black
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
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
简单基本的打标记位和深搜dfs算法,以及范围的判断,但是有一个作者有一个疑惑就是使用STL中的vector<char>就只会出现这种情况,还有使用string vi [ ] 这种情况也会出现同样的问题,为什么???????????
如果我用vector<string> vi[25] 会是什么情况呢???????????(这个STL容器和string vi [25]差不多,而且好像难弄,作者就不弄了)
可能是STL中的 vector<char> vi [25] 和 string vi [25] 存在着某些不为人知的秘密吧
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
7 1
6 1
5 1
4 1
3 1
2 1
1 1
0 1
错误代码
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <deque>
#include <vector>
using namespace std;
//char in[25][25];
vector<string> vi[25];
//for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
int vis[25][25];
//string vi[25];
int x,y;
int sum;
int lie,hang;
int movv[4][2]= { -1,0,1,0,0,-1,0,1 };
void dfs(int x,int y)
{
for(int i=0; i<4; i++)
{
//cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
if( vi[ x+movv[i][0] ][ y+movv[i][1] ] != '#' && vis[ x+movv[i][0] ][ y+movv[i][1] ] == 0 && (x+movv[i][0]>=0) && (x+movv[i][0]<=hang-1) && y+movv[i][1]>=0 && y+movv[i][1]<=lie-1 )
{
sum++;
vis[ x+movv[i][0] ][ y+movv[i][1] ] = 1;
cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
dfs(x+movv[i][0],y+movv[i][1]);
}
}
//return;
}
int main()
{
while(cin>>lie>>hang)
{
sum=0;
string zhong;
memset(vis,0,sizeof(vis));
for(int i=0; i<hang; i++)
{
cin>>zhong;
vi[i].push_back(zhong);
int chang=zhong.size();
for(int j=0; j<chang; j++)
{
if(zhong[j] =='@')
{
x=i;
y=j;
}
}
}
//for(int i=0;i<hang;i++){
// for(int j=0;j<lie;j++)
//cout<<vi[i][j];
//for(vector<string>::iterator it=vi[0].begin();it!=vi[0].end();it++)
//{
//cout<<*it;
//}
cout<<x<<" "<<y<<endl;
vis[x][y]=1;
sum++;
dfs(x,y);
cout<<sum<<endl;
/*for(int i=0; i<hang; i++)
{
for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
cout<<*it;
cout<<endl;
}*/
for(int i=1; i<=hang; i++)
{
vi[i].clear();
}
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <deque>
#include <vector>
using namespace std;
//char in[25][25];
//vector<string> vi[25];
//for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
int vis[25][25];
string vi[25];
int x,y;
int sum;
int lie,hang;
int movv[4][2]= { -1,0,1,0,0,-1,0,1 };
void dfs(int x,int y)
{
for(int i=0; i<4; i++)
{
//cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
if( vi[ x+movv[i][0] ][ y+movv[i][1] ] != '#' && vis[ x+movv[i][0] ][ y+movv[i][1] ] == 0 && (x+movv[i][0]>=0) && (x+movv[i][0]<=hang-1) && y+movv[i][1]>=0 && y+movv[i][1]<=lie-1 )
{
sum++;
vis[ x+movv[i][0] ][ y+movv[i][1] ] = 1;
cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
dfs(x+movv[i][0],y+movv[i][1]);
}
}
//return;
}
int main()
{
while(cin>>lie>>hang)
{
sum=0;
string zhong;
memset(vis,0,sizeof(vis));
for(int i=0; i<hang; i++)
{
cin>>vi[i];
int chang=vi[i].size();
// vi[i].push_back(zhong);
for(int j=0; j<chang; j++)
{
if(vi[i][j] =='@')
{
x=i;
y=j;
}
}
}
//for(int i=0;i<hang;i++){
// for(int j=0;j<lie;j++)
//cout<<vi[i][j];
cout<<x<<" "<<y<<endl;
vis[x][y]=1;
sum++;
dfs(x,y);
cout<<sum<<endl;
/*for(int i=0; i<hang; i++)
{
for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
cout<<*it;
cout<<endl;
}*/
for(int i=1; i<=hang; i++)
{
vi[i].clear();
}
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <deque>
#include <vector>
using namespace std;
//char in[25][25];
//vector<string> vi[25];
//for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
int vis[25][25];
vector<char> vi[25];
int x,y;
int sum;
int lie,hang;
int movv[4][2]= { -1,0,1,0,0,-1,0,1 };
void dfs(int x,int y)
{
for(int i=0; i<4; i++)
{
//cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
if(vi[ x+movv[i][0] ][ y+movv[i][1] ] != '#' && vis[ x+movv[i][0] ][ y+movv[i][1] ] == 0 && (x+movv[i][0]>=0) && (x+movv[i][0]<=hang-1) && y+movv[i][1]>=0 && y+movv[i][1]<=lie-1 )
{
sum++;
vis[ x+movv[i][0] ][ y+movv[i][1] ] = 1;
cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
dfs(x+movv[i][0],y+movv[i][1]);
}
}
//return;
}
int main()
{
/*cout<<movv[0][0];
cout<<movv[0][1]<<endl;
cout<<movv[1][0];
cout<<movv[1][1]<<endl;
cout<<movv[2][0];
cout<<movv[2][1]<<endl;
cout<<movv[3][0];
cout<<movv[3][1]<<endl;*/
/*for(int i=0; i<4; i++)
{
for(int j=0; j<2; j++)
{
cout<<movv[i][j]<<" ";
}
cout<<endl;
}*/
while(cin>>lie>>hang)
{
sum=0;
char zhong;
memset(vis,0,sizeof(vis));
for(int i=0; i<hang; i++)
{
for(int j=0; j<lie; j++)
{
cin>>zhong;
vi[i].push_back(zhong);
if(zhong =='@')
{
x=i;
y=j;
}
}
}
//for(int i=0;i<hang;i++){
// for(int j=0;j<lie;j++)
//cout<<vi[i][j];
cout<<x<<" "<<y<<endl;
vis[x][y]=1;
sum++;
dfs(x,y);
cout<<sum<<endl;
/*for(int i=0; i<hang; i++)
{
for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
cout<<*it;
cout<<endl;
}*/
for(int i=1; i<=hang; i++)
{
vi[i].clear();
}
}
return 0;
}
AC代码
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <deque>
#include <vector>
using namespace std;
//char in[25][25];
//vector<string> vi[25];
//for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
int vis[25][25];
char vi[25][25];
int x,y;
int sum;
int lie,hang;
int movv[4][2]= { -1,0,1,0,0,-1,0,1 };
void dfs(int x,int y)
{
for(int i=0; i<4; i++)
{
//cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
// cout<<vi[x][y]<<" "<<i<<" ";
if(vi[x+movv[i][0]][y+movv[i][1]] != '#' && vis[ x+movv[i][0] ][ y+movv[i][1] ] == 0 && (x+movv[i][0]>=0) && (x+movv[i][0]<=hang-1) && y+movv[i][1]>=0 && y+movv[i][1]<=lie-1 )
{
sum++;
vis[ x+movv[i][0] ][ y+movv[i][1] ] = 1;
//cout<<x+movv[i][0]<<" "<<y+movv[i][1]<<endl;
dfs(x+movv[i][0],y+movv[i][1]);
}
}
return;
}
int main()
{
while(cin>>lie>>hang)
{
if(lie==0&&hang==0)
break;
sum=0;
char zhong;
memset(vis,0,sizeof(vis));
for(int i=0; i<hang; i++)
{
scanf("%s",&vi[i]);
for(int j=0;j<lie;j++)
{
if(vi[i][j]=='@')
{
x=i;
y=j;
}
}
}
/* for(int i=0;i<hang;i++){
for(int j=0;j<lie;j++)
cout<<vi[i][j];
cout<<endl;}
*/
//cout<<x<<" "<<y<<endl;
vis[x][y]=1;
sum++;
dfs(x,y);
cout<<sum<<endl;
/*for(int i=0; i<hang; i++)
{
for(vector<string>::iterator it=vi[i].begin(); it!=vi[i].end(); it++)
cout<<*it;
cout<<endl;
}*/
}
return 0;
}
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13