1. 程式人生 > >杭電Red and Black。。。。水題

杭電Red and Black。。。。水題

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14279    Accepted Submission(s): 8852


Problem Description 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 0  Sample Output 45 59 6 13
#include <iostream>
using namespace std;
char a[22][22];   
int m,n;
int p,q;
int cnt;
void dfs (int x, int y)  
{  
    if (a[x][y]=='#' || x < 1 || x >n|| y < 1 || y > m)  
        return ;  
    ++cnt ;  
    a[x][y] ='#';  
	dfs (x-1, y) ;  
    dfs (x, y-1) ;  
    dfs (x+1, y) ;  
    dfs (x, y+1) ;  
}  
int main ()
{
	
	while(cin>>m>>n)
	{
		int i,j;
		if(m==0&&n==0) break;
		for( i=1;i<=n;i++)
			for( j=1;j<=m;j++)
			{
				cin>>a[i][j];
				if(a[i][j]=='@')
				{
					p=i;
					q=j;
                    a[i][j] = '.' ;  
				}
			}
			
			cnt=0;
			dfs(p,q);
			cout<<cnt<<endl;
	}
	return 0;
}

相關推薦

Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14279    Accepted

OJ-1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26912    Accepted

1000、1001C語言,通過

1000 #include <stdio.h> #include <stdlib.h> int main() { int i,j; while(scanf("

HDU1312 Red and Black

fine mil scanf inpu red other mit ets represent Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (

POJ 1979 Red and Black(簡單DFS)

either www enter ont false num present direction roo Red and Black Description There is a rectangular room, covered with square tile

hdoj-1312-Red and Black

man arc others sim track pid for each only red Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav

HDU 1312 Red and Black(bfs,dfs均可,個人傾向bfs)

spec int ger time scrip follow stdio.h stack line 題目代號:HDU 1312 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Li

HDU - 1312 : Red and Black

getchar() nbsp and stand oid std void tile follow There is a rectangular room, covered with square tiles. Each tile is colored either red

HDU 1979 Red and Black

pro lin itself tip 設置問題 dir repeat ons elf 題目: There is a rectangular room, covered with square tiles. Each tile is colored either red or

POJ 1979 Red and Black (簡單dfs)

++ blog mage dir sin div names turn main 題目: 簡單dfs,沒什麽好說的 代碼: #include <iostream> using namespace std; typedef long long ll

POJ 1979 Red and Black (BFS)

搜索 ont eat size ++ mes 直接 pro eof 鏈接 : Here! 思路 : 簡單的搜索, 直接廣搜就ok了. /*****************************************************************

POJ1979 Red and Black 走黑磚【DFS】

聯通塊 tro problem AC print 要求 pri 聯通 dir 題目鏈接 題目大意: 求圖中最大‘.‘的連通塊,輸出該最大連通塊的塊數 #include <cstdio> int n, m, res; int dir[4][2] = {

POJ 1979 Red and Black

水題 IT sin earch contain tput scanf sample eating Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submission

Hdoj 1312.Red and Black 題解

Problem Description 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. F

解題記錄: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 til

HDU 1312 Red and Black(簡單BFS 聯通塊)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26972&nbs

POJ1979 Red and Black

思路:先將圖存在二維陣列a[20][21]中,標記@的位置為x,y。從@處出發,不停的把鄰接部分用#代替,直到圖中不存在‘.’為止。 總共進行DFS次數就是答案了。4個方向對應四種狀態。 程式碼如下: #include<iostream> using namespace st

Red and Black POJ - 1979

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 c

N - Red and Black ~~ [kuangbin帶你飛]專題一 簡單搜尋

有一個長方形的房間,覆蓋了正方形的磁磚。每塊磁磚的顏色,要麼是紅色,要麼是黑色。一名男子站在一塊黑色的磁磚上。他可以從一塊磁磚移至相鄰四塊磁磚中的某一塊。但是,他不允許在紅色磁磚上移動,他只允許在黑色磁磚上移動。 編寫一個程式,使得他允許重複上述的移動,判斷他所能到達的黑色磁磚的數量。 輸入

【POJ】1979 Red and Black(BFS)

Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 44023 Accepted: 23850 Description There is a