Red and Black
2010-03-08 21:33
176 查看
#include <stdio.h>
struct node{
int x,y; //定义矩阵中的行和列
};
node queue[500];
char g[22][22];
int w,h;
int begin,end; //记录矩阵中的开始和结束的位置
//int count[30],k=0;
int walk[2][4]={{1,0,-1,0},{0,1,0,-1}}; //上下左右四个位置
//找出矩阵中的起点的坐标
void find()
{
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
if(g[i][j]=='@')
{
queue[begin].x=i; //横坐标,即i行
queue[begin].y=j; //纵坐标,即j列
}
}
}
// 输入w和h;即输入列数和行数;
int input()
{
scanf("%d%d",&w,&h);
if(w==0&&h==0)
return w;
else if(w>0&&w<=20&&h>0&&h<=20)
{
for(int i=0;i<h;i++)
scanf("%s",g[i]); //输入每行的字符
return 1;
}
else
return 0;
}
int in(int x2,int y2)
{
if(x2>=0&&x2<h&&y2>=0&&y2<w) //判断是否溢出矩阵
return 1;
else
return 0;
}
void Count()
{
begin=end=1;
find();
while(begin<=end)
{
int x1=queue[begin].x; //得到 @ 的位置
int y1=queue[begin].y;
begin++;
for(int t=0;t<4;t++) //比较右下左上四个位置,矩阵walk为:{1,0,-1,0}
{ // {0,1,0,-1}
int x2=x1+walk[0][t];
int y2=y1+walk[1][t];
if(in(x2,y2))
{
if(g[x2][y2]=='.')
{
g[x2][y2]='-1';
end++; //黑色的加1;
queue[end].x=x2;
queue[end].y=y2; //@ 的位置移动到(x2,y2);
}
}
}
}
printf("%d/n",end); //输出黑色的个数;
}
int main()
{
while(input())
{
Count();
}
return 0;
}
struct node{
int x,y; //定义矩阵中的行和列
};
node queue[500];
char g[22][22];
int w,h;
int begin,end; //记录矩阵中的开始和结束的位置
//int count[30],k=0;
int walk[2][4]={{1,0,-1,0},{0,1,0,-1}}; //上下左右四个位置
//找出矩阵中的起点的坐标
void find()
{
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
if(g[i][j]=='@')
{
queue[begin].x=i; //横坐标,即i行
queue[begin].y=j; //纵坐标,即j列
}
}
}
// 输入w和h;即输入列数和行数;
int input()
{
scanf("%d%d",&w,&h);
if(w==0&&h==0)
return w;
else if(w>0&&w<=20&&h>0&&h<=20)
{
for(int i=0;i<h;i++)
scanf("%s",g[i]); //输入每行的字符
return 1;
}
else
return 0;
}
int in(int x2,int y2)
{
if(x2>=0&&x2<h&&y2>=0&&y2<w) //判断是否溢出矩阵
return 1;
else
return 0;
}
void Count()
{
begin=end=1;
find();
while(begin<=end)
{
int x1=queue[begin].x; //得到 @ 的位置
int y1=queue[begin].y;
begin++;
for(int t=0;t<4;t++) //比较右下左上四个位置,矩阵walk为:{1,0,-1,0}
{ // {0,1,0,-1}
int x2=x1+walk[0][t];
int y2=y1+walk[1][t];
if(in(x2,y2))
{
if(g[x2][y2]=='.')
{
g[x2][y2]='-1';
end++; //黑色的加1;
queue[end].x=x2;
queue[end].y=y2; //@ 的位置移动到(x2,y2);
}
}
}
}
printf("%d/n",end); //输出黑色的个数;
}
int main()
{
while(input())
{
Count();
}
return 0;
}
相关文章推荐
- HDU 1312 Red and Black(简单BFS)
- hdu-1312 Red and Black DFS解法
- poj Red and Black
- HDU1312( Red and Black )
- hdu 1312 Red and Black
- HDU 1312:Red and Black
- Red and Black dfs水题
- 广搜2 —— Red and Black
- Red and Black(基础DFS)
- 【poj1979】Red and Black
- hdu 1312 Red and Black
- HDU 1312 Red and Black --- 入门搜索 DFS解法
- HDU 1312 Red and Black
- hdu 1312 Red and Black
- 杭电 1312 Red and Black
- hdu_1312 Red and Black
- POJ 1979 Red and Black(红与黑)
- hdu Red and Black
- POJ 1979 Red and Black(dfs)
- 杭电Red and Black。。。。水题