题目来源 poj 1562
Description
Input
Output
Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
struct s{
int x,y;
char c;
}map[110][110];
const int dir[8][2]={{1,0},{1,-1},{1,1},{0,1},{0,-1},{-1,-1},{-1,0},{-1,1}};
queue<s> q;
int n,m,ans;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<8;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='@'){
q.push(map[next.x][next.y]);
map[next.x][next.y].c='*';
}
}
}
return ;
}
int main()
{
while (cin>>n>>m&&n&&m){
ans=0;
while (!q.empty())
q.pop();
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='@'){
map[i][j].x=i;
map[i][j].y=j;
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (map[i][j].c=='@'){
q.push(map[i][j]);
map[i][j].c='*';
ans++;
dfs(i,j);
}
}
}
printf("%d
",ans);
}
return 0;
}
Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26317 Accepted Submission(s): 15909
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
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)
ac代码:
#include<iostream> //hdu 1312
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
int n,m,ans;
char temp[25];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s{
int x,y;
char c;
}map[25][25];
queue <s> q;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<4;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='.'&&next.x<=n&&next.y<=m){
ans++;
q.push(map[next.x][next.y]);
map[next.x][next.y].c='#';
}
}
}
return ;
}
int main()
{
while (cin>>m>>n&&n&&m){
while (!q.empty())
q.pop();
int start_x,start_y;
ans=1;
for(int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='.'){
map[i][j].x=i;
map[i][j].y=j;
}
else if (map[i][j].c=='@'){
map[i][j].x=start_x=i;
map[i][j].y=start_y=j;
q.push(map[i][j]);
}
}
}
dfs(start_x,start_y);
printf("%d
",ans);
}
return 0;
}