UVa 11561 - Getting Gold

题目:给你一个二维的地图,里面有陷阱‘T‘,金子‘G‘以及墙壁‘#‘,和普通的道路‘.‘,现在已知一个人在起点‘P‘;

每次只能走到上下左右4个方向的格子,如果走到一个格子周围有陷阱,人就会停下不走,防止陷阱;

问最多能得到多少金子。

分析:图论,搜索。直接利用dfs求解即可,把‘T‘周围的点标记成停止即可。

说明:╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

char maps[55][55];
int  dxy[4][2] = {0,1,1,0,-1,0,0,-1};

bool in_map(int x, int y, int row, int column)
{
	return x >= 0 && x < row && y >= 0 && y < column;
}
int  dfs(int x, int y, int row, int column)
{
	int sum = 0;
	if (in_map(x, y, row, column) == false || maps[x][y] == '#') {
		return sum;
	}

	sum = maps[x][y] == 'G';
	maps[x][y] = '#';

	int stop = false;
	for (int i = 0; i < 4; ++ i) {
		if (in_map(x+dxy[i][0], y+dxy[i][1], row, column)== true &&
				maps[x+dxy[i][0]][y+dxy[i][1]] == 'T') {
			stop = true;
		}
	}
	if (stop == true) {
		return sum;
	}

	for (int i = 0; i < 4; ++ i) {
		sum += dfs(x+dxy[i][0], y+dxy[i][1], row, column);
	}
	return sum;
} 

int main()
{
	int row,column;
	while (~scanf("%d%d",&column,&row)) {
		for (int i = 0; i < row; ++ i) {
			scanf("%s",maps[i]);
		}

		//寻找起点开始搜索
		int find_p = false;
		for (int i = 0; i < row; ++ i) {
			for (int j = 0; j < column; ++ j) {
				if (maps[i][j] == 'P') {
					printf("%d\n",dfs(i, j, row, column));
					find_p = true;
					break;
				}
			}
			if (find_p == true) break;
		}
	}
    return 0;
}
时间: 2024-10-12 18:24:36

UVa 11561 - Getting Gold的相关文章

UVA 11292 Dragon of Loowater(简单贪心)

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

uva 11292

A - Dragon of Loowater Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11292 Appoint description:  System Crawler  (2014-11-26) Description Problem C: The Dragon of Loowater Once upon a time, in the K

UVA之11292 Dragon of Loowater

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

UVA - 11983 Weird Advertisement (线段树求并面积)

Description G Weird Advertisement Renat Mullakhanov (rem), one of the most talented programmers in the world, passed away on March 11, 2011. This is very sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold m

·UVa」 11292 - Dragon of Loowater( 贪心 )

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

UVA它11292 - Dragon of Loowater

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

UVA 题目11292 Dragon of Loowater

Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predato

UVA - 1323 Vivian&#39;s Problem

Description The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliestdocumented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivatedby r

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d