uva 10047 uva live 2035 The Monocycle bfs

2024-04-09 16:08
文章标签 bfs live uva 2035 10047 monocycle

本文主要是介绍uva 10047 uva live 2035 The Monocycle bfs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

// uva 10047 uva live 2035 bfs
// 求最短的嘛,肯定先尝试bfs啦
// 确定状态,首先状态里面得有坐标x,y
// 还得有朝向,还得有颜色值
// 
// 这样就是一个状态里面有着三种属性
// 每个状态都只要经历一次,再经历是没有任何意义的
// 用一个que的思维数组记录就行了。
// 按照方向爆搜,我先用f[i][j]记录的就是到
// 这一点的最小距离,但是怎么都过不了样例
// 突然明白了,如果只是这样记录最短距离,是不行的
// 因为每次从队列中取出的元素都有可能改变终点的状态
// 所以,应该在状态里面再记录一个属性,那就是时间
// 最后,注意两个样例之间要有空行啊。。。wa了一发
// 嗯,之后就过了。
// 嗯,继续练吧。。。。#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 = 30; 
char mp[maxn][maxn];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int n,m;
char a[5][10] = {"GREEN","WHITE","BLUE","RED","BLACK"};
struct node {int x,y;int on;int color;int time;
};
int f[maxn][maxn];
bool que[26][26][5][6];
queue<node> q;
void init(){for (int i=0;i<n;i++)scanf("%s",mp[i]);
//	memset(f,-1,sizeof(f));memset(que,0,sizeof(que));while(!q.empty()) q.pop();
}void print(){for (int i=0;i<n;i++){for (int j=0;j<m;j++){printf("%d ",f[i][j]);}puts("");}puts("");
}void solve(int kase){if (kase!=1)puts("");printf("Case #%d\n",kase);int ex,ey;for (int i=0;i<n;i++)for (int j=0;j<m;j++){if (mp[i][j]=='S'){q.push((node){i,j,0,0,0});que[i][j][0][0] = true;//	f[i][j] = 0;}if (mp[i][j]=='T'){ex = i;ey = j;}}int time = -1;int cc = 0;while(!q.empty()){node x = q.front();//	printf("%d %d %d %s\n",x.x,x.y,x.on,a[x.color]);q.pop();if (x.x==ex && x.y==ey && x.color==0){//time = //f[x.x][x.y];time = x.time;break;}for (int i=-1;i<=1;i++){int j = x.on + i;if (j==-1)//左转j = 3;if (j==4)//右转j = 0;if (i==0){//直走int tx = x.x + dx[j];int ty = x.y + dy[j];if (tx<0||tx>=n||ty<0||ty>=m)	continue;if (mp[tx][ty]=='#')	continue;if (!que[tx][ty][j][(x.color+1)%5]){//f[tx][ty] = f[x.x][x.y]+1;q.push((node){tx,ty,j,(x.color+1)%5,x.time+1});que[tx][ty][j][(x.color+1)%5] = true;}}else {//转弯的情况if (!que[x.x][x.y][j][x.color]){f[x.x][x.y]++;q.push((node){x.x,x.y,j,x.color,x.time+1});que[x.x][x.y][j][x.color] = true;}}}//	que[x.x][x.y][x.on][x.color] = false;}//print();if (time == -1){printf("destination not reachable");}else {printf("minimum time = %d sec",time);}puts("");
//	puts("");
}int main() {int kase = 0;//freopen("G:\\Code\\1.txt","r",stdin);while(scanf("%d%d",&n,&m)){if (!n&&!m)break;init();solve(++kase);}return 0;
}

这篇关于uva 10047 uva live 2035 The Monocycle bfs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/888615

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^