练习系统 实验四 red and black

来源:互联网 发布:js中new date 的参数 编辑:程序博客网 时间:2024/04/30 06:02

当前编程题:实验四 基础算法问题(16级)---Red and Black

1.
问题描述
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.
输入形式
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.
输出形式
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).
样例输入
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0
样例输出
45
中文翻译:
问题描述
有一个长方形的房间,用方瓷砖覆盖地面。瓷砖的颜色是红色或黑色。一个人站在一个黑色的瓷砖上。从一个瓷砖可以移动到相邻的四个瓷砖上。但他不能移动到红色瓷砖上,他仅可以在黑色的瓷砖上移动。
编写一个程序来计算他可以到达的黑瓷砖数量。
输入形式
输入由多组数据组成。一组数据由一个包含两个正整数W和H开始;W和H分别是在x和y方向的瓷砖数。W和H不超过20。
有H行的数据,其中每行包括W个字符。每个字符代表一个瓷砖的颜色。
'.' - 黑色瓷砖
'#' - 红色瓷砖
'@' -黑色瓷砖的一个人(在数据中只出现一次)
输入结束: 由两个零组成的数表示。
输出形式

        对于每组数据,你的程序应该输出一行,包含了从最初的瓷砖开始可以达到的瓷砖数量(包括最初的瓷砖)。






#include<iostream>
using namespace std;
int W,H;
char aa[20][20];


int f(int x,int y)
{
if(x<0|| x>=H ||y<0 ||y>=W ||aa[x][y]=='#' )
{
return 0;
}
else
{
aa[x][y]='#';
return (1+f(x-1,y)+f(x+1,y)+f(x,y-1)+f(x,y+1));
}
}




int main()
{
int p,q;
while(1)
{
cin>>W>>H;
if(W==0  ||H==0)
{
break;
}
   for(int i=0;i<H;i++)
    {
    for(int j=0;j<W;j++)
    {
    cin>>aa[i][j];
    if(aa[i][j]=='@')            //啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊  我被这里坑到了  气的我都想砸电脑  怎么也找不出错   竟然是这里         !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!抓狂
    {
    p=i;q=j;
    }
    }
    }
    cout<<f(p,q)<<endl;
}
return 0;
}

0 0