POJ-3083 Children of the Candy Corn(深搜,广搜)

原创 2013年12月05日 12:32:48

Children of the Candy Corn
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8505   Accepted: 3728

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.) 

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#'). 

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

思路:

        题不难,但同时考到了深搜+广搜;

        需要注意,往左优先的深搜和往右优先的深搜搜出来的最小步数不一定是整体的最小步数;求最小步数,必须广搜!


代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#define N 45

using namespace std;

struct Node{
	int x;
	int y;
	int s;
	int fx;
};

char map[N][N];
bool bz[N][N];
int m, n;
queue<Node>q;
int fx[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int f[4][4][2] = {
	{{-1, 0}, {1, 0}, {0, 1}, {0, -1}},
	{{0, 1}, {0, -1}, {1, 0}, {-1, 0}},
	{{1, 0}, {-1, 0}, {0, -1}, {0, 1}},
	{{0, -1}, {0, 1}, {-1, 0}, {1, 0}},
};

int bfs();
int dfs(Node start, int fxi);
void getfx(Node &cur);

int main()
{
	int k;
	scanf("%d", &k);
	while(k --){
		while(!q.empty()) q.pop();
		memset(map, '#', sizeof(map));
		memset(bz, 0, sizeof(bz));
		scanf("%d%d", &m, &n);
		Node start;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= m; j ++){
				scanf(" %c", &map[i][j]);
				if(map[i][j] == 'S'){
					start.x = i;
					start.y = j;
					start.s = 1;	
					q.push(start);
					map[i][j] = '#';
				}
			}
		}
		getfx(start);
		int minl = dfs(start, 0);
		int minr = dfs(start, 1);
		int mins = bfs();
		printf("%d %d %d\n", minl, minr, mins);
	}

	return 0;
}

int bfs()
{
	Node cur;
	while(1){
		cur.x = q.front().x;
		cur.y = q.front().y;
		cur.s = q.front().s + 1;
		q.pop();

		for(int i = 0; i < 4; i ++){
			Node nw = cur;
			nw.x += fx[i][0];
			nw.y += fx[i][1];
			if(map[nw.x][nw.y] == '#' || bz[nw.x][nw.y]) continue;
			if(map[nw.x][nw.y] == 'E') return nw.s;
			bz[nw.x][nw.y] = 1;
			q.push(nw);
		}

		if(q.empty()) return -1;
	}
}

int dfs(Node cur, int fxi)
{
	Node nw;
	int i = cur.fx;
	int ok;
	int x = cur.x + f[i][fxi][0];
	int y = cur.y + f[i][fxi][1];
	char c = map[x][y];
	if(c == 'E'){				// fxi 方向
		return cur.s + 1;
	}
	else if(c == '.'){
		nw.x = x;
		nw.y = y;
		nw.s = cur.s + 1;
		if(fxi == 0) nw.fx = (cur.fx + 3) % 4;
		else nw.fx = (cur.fx + 1) % 4;
		ok = dfs(nw, fxi);
		if(ok) return ok;
	}
	else{
		x = cur.x + f[i][2][0];
		y = cur.y + f[i][2][1];
		c = map[x][y];
		if(c == 'E'){		// cur.fx 方向
			return cur.s + 1;
		}
		else if(c == '.'){
			nw.x = x;
			nw.y = y;
			nw.s = cur.s + 1;
			nw.fx = cur.fx;
			ok = dfs(nw, fxi);
			if(ok) return ok;
		}
		else{
			int nfx = (fxi + 1) % 2;
			x = cur.x + f[i][nfx][0];
			y = cur.y + f[i][nfx][1];
			c = map[x][y];
			if(c == 'E'){		// - fxi 方向
				return cur.s + 1;
			}
			else if(c == '.'){
				nw.x = x;
				nw.y = y;
				nw.s = cur.s + 1;
				if(nfx == 0) nw.fx = (cur.fx + 3) % 4;
				else nw.fx = (cur.fx + 1) % 4;
				ok = dfs(nw, fxi);
				if(ok) return ok;
			}
			else{				// -cur.fx 方向
				nw.x = cur.x + f[i][3][0];
				nw.y = cur.y + f[i][3][1];
				nw.s = cur.s + 1;
				nw.fx = (cur.fx + 2) % 4;
				ok = dfs(nw, fxi);
				if(ok) return ok;
			}
		}
	}

	return 0;
}

void getfx(Node &cur)
{
	if(cur.x == 1) cur.fx = 1;
	if(cur.x == n) cur.fx = 3;
	if(cur.y == 1) cur.fx = 0;
	if(cur.y == n) cur.fx = 2;
}

	

corn表达式生成器

css代码: style.css /* * Licensed to the Apache Software Foundation (ASF) under one or more * cont...
  • qq_22898247
  • qq_22898247
  • 2016年12月09日 18:48
  • 497

【LeetCode】Candy 解题报告

There are N children standing in a line. Each child is assigned a rating value. You are giving cand...
  • ljiabin
  • ljiabin
  • 2014年09月04日 22:46
  • 4421

Children of the Candy Corn(新颖题目和BFS最短路,没事可以拿出来品品)

Children of the Candy Corn Description The cornfield maze is a popular Halloween treat. Visitors...
  • Bepthslowly
  • Bepthslowly
  • 2016年11月11日 20:57
  • 166

M - Children of the Candy Corn (第一次写左右优先的搜索,自己独立写的可能不够优化)

M - Children of the Candy Corn The cornfield maze is a popular Halloween treat. Visitors are shown ...
  • nhl19961226
  • nhl19961226
  • 2017年01月09日 18:38
  • 117

Spring定时器corn表达式详解

 1.结构 Spring定时器corn表达式详解 2.各字段的含义   字段 允许值 允许的特殊字符 秒 0~59 - * / 分 ...
  • hp_yangpeng
  • hp_yangpeng
  • 2017年05月04日 16:04
  • 1364

Quartz中时间表达式的设置-----corn表达式

Quartz中时间表达式的设置-----corn表达式 时间格式: , 分别对应: 秒>分>小时>日>月>周>年, 举例: 1.每天什么时候执行: 0 59 23 * * ?: 如...
  • XIAyuzheng
  • XIAyuzheng
  • 2013年11月12日 17:51
  • 2330

Quartz中时间表达式的设置-----corn表达式

Quartz中时间表达式的设置-----corn表达式 时间格式: ,   分别对应: 秒>分>小时>日>月>周>年,  举例: 1.每天什么时候执行:        0 59 23 * ...
  • yzh_2017
  • yzh_2017
  • 2017年01月12日 15:31
  • 546

Alien Skin Eye Candy v7.x(眼睛糖果7)最新通用汉化补丁32/64位

Alien Skin Eye Candy v7.x(眼睛糖果7)主要应用对象包含各种设计任务,字体,标志,网页设计等,通过对自然现象的模拟提供各种现实的精致效果。眼睛糖果 的界面简单直观,轻松提高你使...
  • xwm0008
  • xwm0008
  • 2014年06月09日 11:12
  • 3177

quartz中的corn表达式(转)

http://wenku.baidu.com/view/27ecfe32ee06eff9aef8072a.html     quartz中的corn表达式(转) 分类: J2EE 2012...
  • dusj
  • dusj
  • 2014年06月17日 20:21
  • 897

Quartz中时间表达式的设置-----corn表达式

总是忘记,特摘录了一篇别人写的总结,首先感谢原作者 Quartz中时间表达式的设置-----corn表达式...
  • lu_yongchao
  • lu_yongchao
  • 2016年05月11日 16:46
  • 801
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ-3083 Children of the Candy Corn(深搜,广搜)
举报原因:
原因补充:

(最多只允许输入30个字)