POJ 3984 迷宫问题

7 篇文章 0 订阅
5 篇文章 0 订阅

题目通道

瞎撞式的 dfs
 #include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
	int a,b; 
}anse[100],dfse[100];
int ans = inf; 
void dfs(int x,int y,int dep){
	if ( x == 4 && y == 4 ){
		if ( ans > dep ){
			ans = dep; 
			rep(i,1,dep)
				anse[i].a = dfse[i].a,
				anse[i].b = dfse[i].b;
		}
		return ;
	}
	if ( x+1<=4 && e[x+1][y] == 0 ){
		dfse[dep+1].a = x+1; 
		dfse[dep+1].b = y; 
		dfs(x+1,y,dep+1);		
	}
	if ( y+1<=4 && e[x][y+1] == 0 )
	{
		dfse[dep+1].a = x; 
		dfse[dep+1].b = y+1; 		
		dfs(x,y+1,dep+1);		
	}
		
}
int main()
{
//	freopen("1.txt","r",stdin);
//	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
//	int  n; 
	rep(i,0,4)	rep(j,0,4)
		scanf("%d",&e[i][j]);
	anse[1].a = anse[1].b = 0;
	dfs(0,0,1);
	rep(i,1,ans){
		printf("(%d, %d)",anse[i].a,anse[i].b);
		if ( ans != i )	
			cout<<endl; 
	}
	return 0;
}
迭代dfs 相对暴力
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
	int a,b; 
}anse[100],dfse[100];
int ans = inf; 
bool vis[N][N];
bool isin ( int x,int y){
	if ( 0 <= x && x <= 4 && 0 <= y && y <= 4 )	return true; 
	return false; 
}
int nxt[][2] = {1,0,-1,0,0,1,0,-1};
bool ok = 0; 
void dfs(int x,int y,int dep,int lim){
	if ( dep > lim )	return ; 
	if ( x == 4 && y == 4 ){
		if ( ans > dep ){
			ok = 1; 
			ans = dep; 
			rep(i,1,dep)
				anse[i].a = dfse[i].a,
				anse[i].b = dfse[i].b;
		}
		return ;
	}
	rep(i,0,3){
		int nx,ny; 
		nx = x + nxt[i][0];
		ny = y + nxt[i][1];
		if ( isin(nx,ny) && e[nx][ny] == 0 && vis[nx][ny] == 0){
			vis[nx][ny] = 1; 
			dfse[dep].a = nx;
			dfse[dep].b = ny;  
			dfs(nx,ny,dep+1,lim);
			vis[nx][ny] = 0; 
		}
	}
}
int main()
{
	freopen("1.txt","r",stdin);
//	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
//	int  n; 
	rep(i,0,4)	rep(j,0,4)
		scanf("%d",&e[i][j]);
	anse[0].a = anse[0].b = 0;
	vis[0][0] = 1; 
	memset(vis,0,sizeof(vis));
	for (int i = 1; i <= 29; i++){
		if ( ok == 0 )
			dfs(0,0,1,i);		
	}

	rep(i,0,ans-1){
		printf("(%d, %d)",anse[i].a,anse[i].b);
		if ( ans != i )	
			cout<<endl; 
	}
	return 0;
}
迭代dfs 很暴力
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<ctime>
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define LL long long 
#define eps 0.000001
#define inf 0x3f3f3f3f
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=10*x+a-'0'; a=getchar();}return x*y;
}
#define N 9
int e[N][N];
struct {
	int a,b; 
}anse[100],dfse[100];
int ans = inf; 
bool vis[N][N];
bool isin ( int x,int y){
	if ( 0 <= x && x <= 4 && 0 <= y && y <= 4 )	return true; 
	return false; 
}
int nxt[][2] = {1,0,-1,0,0,1,0,-1};
void dfs(int x,int y,int dep){
	if ( x == 4 && y == 4 ){
		if ( ans > dep ){
			ans = dep; 
			rep(i,1,dep)
				anse[i].a = dfse[i].a,
				anse[i].b = dfse[i].b;
		}
		return ;
	}
	rep(i,0,3){
		int nx,ny; 
		nx = x + nxt[i][0];
		ny = y + nxt[i][1];
		if ( isin(nx,ny) && e[nx][ny] == 0 && vis[nx][ny] == 0){
			vis[nx][ny] = 1; 
			dfse[dep].a = nx;
			dfse[dep].b = ny;  
			dfs(nx,ny,dep+1);
			vis[nx][ny] = 0; 
		}
	}
//	if ( x+1<=4 && e[x+1][y] == 0 ){
//		dfse[dep+1].a = x+1; 
//		dfse[dep+1].b = y; 
//		dfs(x+1,y,dep+1);		
//	}
//	if ( y+1<=4 && e[x][y+1] == 0 )
//	{
//		dfse[dep+1].a = x; 
//		dfse[dep+1].b = y+1; 		
//		dfs(x,y+1,dep+1);		
//	}
//		
}
int main()
{
	freopen("1.txt","r",stdin);
//	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
//	int  n; 
	rep(i,0,4)	rep(j,0,4)
		scanf("%d",&e[i][j]);
	anse[0].a = anse[0].b = 0;
	vis[0][0] = 1; 
	memset(vis,0,sizeof(vis));
	dfs(0,0,1);
	rep(i,0,ans-1){
		printf("(%d, %d)",anse[i].a,anse[i].b);
		if ( ans != i )	
			cout<<endl; 
	}
	return 0;
}
标准解法 bfs

不写代码,so easy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值