poj 3009 Curling 2.0 dfs回溯

// poj3009 Curling 2.0
// dfs水题,开始的时候没有想到在走了10步以后就不走了这个重要的剪枝,
// 结果tle了。。。
// 后来想了个vis数组记录走过的路径,结果发现并不能这样标记,因为每个点可能
// 走多次,所以这样是不对的
//
// 哎,继续练吧,水题都差不多搜了一个小时,哎,。。。

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L);

template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; }

const int maxn = 32;
int g[maxn][maxn];
int n,m;
int vis[maxn][maxn];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int t;
int mi;
int nowx,nowy;
const int inf = 0x3f3f3f3f;

void print(){
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			printf("%d ",g[i][j]);
		}
		puts("");
	}
}

void init(){
	mi = inf;
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++){
			scanf("%d",&g[i][j]);
			if (g[i][j]==3){
				nowx = i;
				nowy = j;
			}
		}
	memset(vis,0,sizeof(vis));

}
void dfs(int x,int y,int cnt){

//	print();
//	puts("");
//	vis[x][y] = 1;
	if (cnt>10){ //题目有要求
		return ;
	}
	if (x==nowx&&y==nowy){
		mi = min(cnt,mi);
		vis[x][y] = 0;
		return ;
	}
	//vis[x][y] = 1;
	for (int i=0;i<4;i++){
		int tx = x + dx[i];
		int ty = y + dy[i];
		if (tx<0||tx>=n||ty<0||ty>=m) // 判断越界
			continue;
		if (g[tx][ty]==1) //冰壶需要隔一个才能推动
			continue;
		for (int k=1;k<=max(n,m);k++){
			tx = x + dx[i] * k;
			ty = y + dy[i] * k;
			if (tx<0||tx>=n||ty<0||ty>=m)
				continue;
			if (g[tx][ty]==1){
			//	int t = g[tx][ty];
			//	vis[tx][ty] = 1;
				g[tx][ty] = 0;
				dfs(tx-dx[i],ty-dy[i],cnt+1);
				g[tx][ty] = 1;
				break;
			}else if (g[tx][ty]==3){
				dfs(tx,ty,cnt+1);
			//	break;
			}
		}
	}
	//vis[x][y] = 0;
}

void solve(){
	for (int i=0;i<n;i++)
		for (int j=0;j<m;j++)
			if (g[i][j]==2)
				dfs(i,j,0);
	if (mi==inf)
		mi = -1;
	printf("%d\n",mi);
}

int main() {
	//freopen("G:\\Code\\1.txt","r",stdin);
	while(scanf("%d%d",&m,&n)!=EOF){
		if (!m&&!n)
			break;
		init();
		solve();
	}
	return 0;
}

时间: 2024-08-05 19:36:10

poj 3009 Curling 2.0 dfs回溯的相关文章

poj 3009 Curling 2.0 (dfs)

id=3009">链接:poj 3009 题意:在一个冰面网格板上,有空白处(无障碍),和障碍块.有一个小石头,给定其起点和终点.求从起点到终点的最小步数 规则:小石头不能在障碍区运动,一旦从某一方向開始运动,不会改变方向,也不会停止.除非碰到障碍物或到达终点才会停止,这为一步.若碰到障碍物.小石头将停在障碍物的旁边,被碰到的一个障碍物将消失. 输入:1代表障碍物(不可到达),0代表空白区,2,代表起点.3代表终点 输出:若小石头能到达终点,且步数最多为十步,输出最小步数,否则输出-1.

POj 3009 Curling 2.0(DFS + 模拟)

题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0”和“1”组成,目的是计算从“2”走到“3”的最短步数,“1”代表障碍物,“0”代表可以通行.“2”可以往周围四个方向走,如果往某一个方向走,那么停下来的条件是,当这个方向上存在障碍物“1”,且会停在这个障碍物的前一个格子,并会击碎这个障碍物;如果选择的方向上没有障碍物“1”也没有终点“3”,那么就会

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 3009 Curling 2.0 (dfs)

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12700   Accepted: 5343 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj 3009 Curling 2.0 (dfs )

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj 3009 Curling 2.0 深搜

http://poj.org/problem?id=3009 题意:一个小球在一个格子里滑行,当你给它一个力时,他会一直滑,直到前方碰到一个雪球停止,这时前方的雪球会消失,你继续给该小球任意一个方向的力...问至少需要几步才能到达到终点. 分析: 一般在求  最短路    时会用到   广搜,但是  本题  在搜索时, 每走一步, 现场状态是需要改变的 ,如果该步不满足,又需要把现场状态还原回去  ,这样   深搜  才能满足 因此用  深搜     只能把   所有能到达终点的路的步数    

POJ 3009 Curling 2.0

Curling 2.0 Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 300964-bit integer IO format: %lld      Java class name: Main On Planet MM-21, after their Olympic games this year, curling is getting popular. But

POJ 3009 Curling 2.0 {广度优先搜索}

原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose