第十届蓝桥迷宫题

这题和POJ的迷宫题基本一样,赛前一周还碰到过,可惜自己当时水题了,就没认真做,真到碰到时自己却只能跳过,还是怪自己不认真吧,做题太少又太水,经过这次比赛,也算是接受教训学习吧。

#include <iostream>
#include <algorithm> 
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <bitset> 
#include <unordered_map>
#include <unordered_set>
#define ll long long
#define vi vector<int> 
#define si set<int>
#define pi pair<int,int> 
#define pii pair<ll,ll> 
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define sci(x) scanf("%d",&(x))
#define scll(x) scanf("%lld",&(x))
#define sclf(x) scanf("%lf",&(x))
#define pri(x) printf("%d",(x))
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define mem(a,b) memset(a,b,sizeof(a)) 
using namespace std;

int main()
{
	char  res[1000];
 	int a[300][300];
 	rep(i,0,29){
 		rep(j,0,49){
 			cin>>a[i][j];
		 }
	 }
	 //bfs
	 queue<pair<int,int>> q;	 	
	 map<pi,char> m;	
	 map<pi,pi> p;//记录前驱	
     q.push(mp(0,0));
     int cnt=0;
     while(!q.empty()){
     	int n = q.size();
     	rep(j,1,n){
     		pi temp = q.front();
     		q.pop();
     		if(temp.first==29&&temp.second==49){
     			
     			while(temp.first||temp.second){
     				res[cnt++] = m[temp];
     				temp = p[temp];
				 }
				 break;
			 }
     		if(temp.first+1>=0&&temp.first+1<30&&temp.second>=0&&temp.second<50&&a[temp.first+1][temp.second]==0){
     			q.push(mp(temp.first+1,temp.second));
     			m[mp(temp.first+1,temp.second)] = 'D';
     			p[mp(temp.first+1,temp.second)] = temp;
     			a[temp.first+1][temp.second]=1;
			 }
			 if(temp.first>=0&&temp.first<30&&temp.second-1>=0&&temp.second-1<50&&a[temp.first][temp.second-1]==0){
			 	q.push(mp(temp.first,temp.second-1));
			 	m[mp(temp.first,temp.second-1)] = 'L';
			 	p[mp(temp.first,temp.second-1)] = temp;
			 	a[temp.first][temp.second-1]=1;
			 }
			 if(temp.first>=0&&temp.first<30&&temp.second+1>=0&&temp.second+1<50&&a[temp.first][temp.second+1]==0){
			 	q.push(mp(temp.first,temp.second+1));
			 	m[mp(temp.first,temp.second+1)] = 'R';
			 	p[mp(temp.first,temp.second+1)] = temp;
			 	a[temp.first][temp.second+1]=1;
			 }
			 if(temp.first-1>=0&&temp.first-1<30&&temp.second>=0&&temp.second<50&&a[temp.first-1][temp.second]==0){
     			q.push(mp(temp.first-1,temp.second));
     			m[mp(temp.first-1,temp.second)] = 'U';
     			p[mp(temp.first-1,temp.second)] = temp;
     			a[temp.first-1][temp.second]=1;
			 }
		 }
	 }
	 per(i,cnt-1,0){
	 	printf("%c",res[i]);
	 }
    return 0;
}
//00101001010101101001010100011010101101110000110101
//11001010000100001100000010100101000001000111000010
//00001000110000110101101000000100101001001000011101
//10100101000101000000001110110010110101101010100001
//00101000010000110101010000100010001001000100010101
//10100001000110010001000010101001010101011111010010
//00000100101000000110010100101001000001000000000010
//11010000001001110111001001000011101001011011101000
//00000110100010001000100000001000011101000000110011
//10101000101000100010001111100010101001010000001000
//10000010100101001010110000000100101010001011101000
//00111100001000010000000110111000000001000000001011
//10000001100111010111010001000110111010101101111000

结果:DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR(186个)

猜你喜欢

转载自blog.csdn.net/Csdn_jey/article/details/88782353