1. 程式人生 > >ZOJ—— 2165 Red and Black(搜尋)

ZOJ—— 2165 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. 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 integersW and H; W and H are the numbers of tiles in thex- 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<stdio.h> 
#include<string.h>
int f[110][110];
char e[110][110];
int a,b,c,i,j;
void q(int x,int y);
int main()
{
	
	while(scanf("%d%d",&a,&b),a !=0 || b != 0){
		c = 0;
		memset(f,0,sizeof(f));
		gets(e[105]);
		for(i = 0;i < b;i ++)
			gets(e[i]);
		for(i = 0;i < b;i ++){
			for(j = 0;j < a;j ++){
				if(e[i][j] == '@'){
					c = 1;
					break;
				}
			}
			if(c)
				break;
		}
		c = 0;
		q(i,j);
		f[i][j] = 1;
		printf("%d\n",c);
	}
	return 0;
}
void q(int x,int y)
{
	int i;
	int next[4][2]={0,1,
					1,0,
					0,-1,
					-1,0};
	e[x][y] = '-';
	c ++;
	for(i = 0;i < 4;i ++){
		int tx = x + next[i][0];
		int ty = y + next[i][1];
		if(tx >= b || tx < 0 || ty >= a || ty < 0 )
			continue;
		if(e[tx][ty] == '.' && !f[tx][ty]){
			f[tx][ty] = 1;
			q(tx,ty);
			
		}
	}
	return ;
}

相關推薦

ZOJ—— 2165 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

Red and Black搜尋題目

Red and Black Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %I64d & %I64u Submit   Status Description There is a rectangula

【POJ】1979 Red and BlackBFS

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

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 bl

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

HDU 1312 Red and Blackbfs,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

ACM:POJ-1979 Red And BlackJAVA的字元陣列輸入以及標記方法

對於該題目直接用DFS或者BFS都可以直接暴力出來,實際上題目也不難,只需要遞迴一個搜尋方法即可。那麼用C或者C++將很容易的解決這個題目。 但是如果用JAVA寫的話將存在一個昨晚讓我糾結了很久的問題:輸入資訊需要以字元陣列的形式儲存,那麼如何解決標記以及儲存的問題? 剛開

HDU 1312Red and Blackdfs

Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 1   Accepted Subm

搜尋1廣度優先Red and Black

Red and Black Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Su

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

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

ZOJ 3825 Garden and Sprinklers直線與圓相交

暫存【不好意思,TLE了】 #include <iostream> #include <stdlib.h> #include <stdio.h> #include <math.h> #include <

HDU 5054 Alice and Bob數學

esp contain before mod see min roc axis factor 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5054 Problem Description Bob and A

Chapter 7 Integrity完整性, Views視圖, Security安全性, and Catalogs目錄

ctu 具體實現 grant rtu con 空值 ont tro access from Database Design to Physical Form CREATE TABLE integrity constraints (完整性約束) CREATE VIEW S

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 (

[Codeforces Round #261 (Div. 2) E]Pashmak and GraphDp

solution and other main ems scanf homework max urn Description Pashmak‘s homework is a problem about graphs. Although he always tries

UVA10491 - Cows and Cars概率

概率題 ref block 題目 tom 告訴 top courier isp UVA10491 - Cows and Cars(概率) 題目鏈接 題目大意:給你n個門後面藏著牛。m個門後面藏著車,然後再給你k個提示。在你作出選擇後告訴你有多少個門後

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

Codeforce 588A - Duff and Meat 貪心

ade ram each ini pri nts sin spec total Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in

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