poj 1324 Holedox Moving

poj 1324 Holedox Moving

题目地址: http://poj.org/problem?id=1324

题意: 给出一个矩阵中,一条贪吃蛇,占据L长度的格子, 另外有些格子是石头, 不能通过, 请问蛇到达 (1,1)格子最短距离。

明显的BFS问题, 将每一个可以走的格子进入队列,

格子判断能否走?(给蛇身体标序号(蛇头l, 蛇尾1,其他置为0), 当 fabs(cur_num - tmp_num)>=l 的时候,说明蛇的身体已经离开之前的格子了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 50;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0}; 

int n,m,l, mp[maxn][maxn];
bool visited[maxn][maxn];
int queuex[maxn*maxn], queuey[maxn*maxn]; 

int main(){
	freopen("in.txt", "r", stdin); 

	int i,j, x, y, k, head, tail, ans, cnt=1;
	bool flag;
	int tmp_x, tmp_y, tmp_val,  cur_x, cur_y, cur_val;
	while(scanf("%d %d %d", &n, &m, &l) != EOF){
		if(n==0 && m==0 && l==0){
			break;
		}
		memset(mp, 0, sizeof(mp));
		memset(visited, false, sizeof(visited));
		head = tail = 0;
		for(i=0; i<l; i++){
			scanf("%d %d", &x, &y);
			if(i == 0){
				queuex[head] = x;
				queuey[head++] = y;
			}
			mp[x][y] = l-i;
		}
		scanf("%d", &k);
		for(i=0; i<k; i++){
			scanf("%d %d", &x, &y);
			visited[x][y] = true;
		}
		ans = -1;   flag = false;
		while(head >= tail){
			if(flag){ break; }
			tmp_x = queuex[tail]; tmp_y = queuey[tail++];
			tmp_val = mp[tmp_x][tmp_y];
			for(i=0; i<4; i++){
				cur_x = dx[i] + tmp_x;
				cur_y = dy[i] + tmp_y;
				if(cur_x == 1 && cur_y == 1){
					ans = tmp_val + 1;
					flag = true;
					break;
				}
				if( cur_x >=1 && cur_x <= n && cur_y>=1 && cur_y<=m && 					!visited[cur_x][cur_y] && fabs(mp[cur_x][cur_y] - tmp_val)>=l){
					queuex[head] = cur_x;
					queuey[head++] = cur_y;
					mp[cur_x][cur_y] = tmp_val + 1;
				}
			}
		}
		if(ans != -1){
			printf("Case %d: %d\n", cnt++, ans-l);
		}else{
			printf("Case %d: %d\n", cnt++, ans);
		}
	}
	return 0;
}

  

时间: 2024-08-05 23:19:37

poj 1324 Holedox Moving的相关文章

poj 1324 Holedox Moving A*算法对bfs的优化

题意: 迷宫里有一条贪食蛇,求它的蛇头到迷宫左上角最少要多少步. 分析: 关键是将蛇的状态压缩编码,然后bfs,超时就改A*,这题有类似最短路径的性质,A*发现节点重复后不需要更新直接舍弃即可. 代码: //poj 1324 //sep9 #include <iostream> #include <algorithm> #include <queue> using namespace std; struct state { int x[10],y[10]; }; str

POJ 1324 [Holedox Moving] 状态压缩BFS

题目链接:http://poj.org/problem?id=1324 题目大意:n*m网格里有蛇和障碍,蛇只能向空格处移动,不能撞到自己,问到(1,1)的最短步数,如无法到达输出-1. 关键思想:不能直接对蛇头进行BFS,因为蛇身对蛇头的决策也有影响,故BFS时应该保存蛇身状态,我们放在state里用位运算来做.因为根据蛇头和每一节延伸的方向(东南西北)我们可以还原出整条蛇.比较坑的是这道题用pow会超时,自己写一个好啦. 由于vis数组很大,如果对于每个Case都初始化vis数组浪费时间,所

POJ 1324 Holedox Moving 贪吃蛇 状态压缩 BFS

Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life. Holedox is a special snake, but its body is not very long.

poj 1324 状态压缩+bfs

http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17042   Accepted: 4065 Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes

poj1324 Holedox Moving

Holedox Moving Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16980   Accepted: 4039 Description During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its l

UVALive 2520 Holedox Moving(BFS+状态压缩)

这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我用string将蛇的各个位置都存下来,用map记录这个状态有没有出现过,当时是过了题目中给的样例,我就开始担心STL会不会超时,后来发现想多了,这个方法WA了,至于为什么,我还没明白,或许是状态的处理错误.总之我自己也觉得这个状态有点不靠谱..于是开始换用题解的方式.发现所谓的进制哈希,其实就是状态

BFS专题

题目编号 OJ编号 题目名称 题解链接 Problem A HDU 1548 A strange lift http://www.cnblogs.com/ohyee/p/5389459.html Problem B HDU 1372 Knight Moves http://www.cnblogs.com/ohyee/p/5389471.html Problem C HDU 2717 Catch That Cow http://www.cnblogs.com/ohyee/p/5389479.htm

BFS学习总结

BFS学习总结 给你一个n*m的网格迷宫,迷宫中有些格子不能走,其他的格子都能走.然后给你起点与终点,问你从起点走到终点最少需要多少步? 上面的问题就是一个典型的BFS问题,对于这类问题来说,只要你掌握了这类问题的关键思想,其实他们都是可以用类似的思路来做的. 你可以把BFS问题想象成:从一个父亲(起点状态)生儿子(后继状态),儿子又生孙子(后继状态)的过程,只要这个家族中出生了一个满意的后代(终点状态),这个家族就不生了. 但是如果这个家族中有两个完全一样的人出生(他们的辈分不一定相同),那么

搜索题推荐

(转自网络博客): POJ POJ 1376 – Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=1376 题意:略 解法:bfs,A*-. POJ 2688 – Cleaning Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=2688 题意:bfs后转换为tsp问题 解法:bfs+dp,bfs+dfs 相关:http://hi.baidu.com/zfy0701/blo