NOJ 题目1642 简单的图论问题?(BFS)
来源:互联网 发布:单片机 控制 四轴 编辑:程序博客网 时间:2024/06/06 03:09
[1642] 简单的图论问题?
- 时间限制: 5000 ms 内存限制: 65535 K
- 问题描述
给一个 n 行 m 列的迷宫,每个格子要么是障碍物要么是空地。每个空地里都有一个权值。你的 任务是从找一条(r1,c1)到(r2,c2)的路径,使得经过的空地的权值之和最小。每一步可以往上下 左右四个方向之一移动一格,但不能斜着移动,也不能移动到迷宫外面或者进入障碍物格子。
如下图,灰色格子代表障碍物。路径 A->B->D->F->E 的权值为 10+3+6+14+8=41,它是从 A 到 E 的最优路径。注意,如果同一个格子被经过两次,则权值也要加两次。
为了让题目更有趣(顺便增加一下难度),你还需要回答另外一个问题:如果你每次必须转弯 (左转、右转或者后退,只要不是沿着上次的方向继续走即可),最小权值是多少?比如,在 上图中,如果你刚刚从 A 走到 B,那么下一步你可以走到 D 或者 A,但不能走到 G。在上图 中,A 到 E 的最优路径是 A->B->D->H->D->F->E,权和为 10+3+6+2+6+14+8=49。注意,D 经 过了两次。
- 输入
- 输入包含不超过 10 组数据。每组数据第一行包含 6 个整数 n, m, r1, c1, r2, c2 (2<=n,m<=500, 1<=r1,r2<=n, 1<=c1,c2<=m). 接下来的 n 行每行包含 m 个格子的描述。每个格子要么是一个 1~100 的整数,要么是星号"*"(表示障碍物)。起点和终点保证不是障碍物。
- 输出
- 对于每组数据,输出两个整数。第一个整数是“正常问题”的答案,第二个整数是“有趣问 题”的答案。如果每个问题的答案是“无解”,对应的答案应输出-1。
- 样例输入
4 4 1 2 3 27 10 3 9 * 45 6 2 * 8 14 * 21 1 * * 2 4 1 1 1 41 2 3 49 * * 92 4 1 1 1 41 * 3 49 9 * 9
- 样例输出
Case 1: 41 49Case 2: 10 -1Case 3: -1 -1
- 提示
无
- 来源
第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛
- 操作
ac代码 #include<stdio.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<string.h>#include<string>#include<queue>#define LL long longusing namespace std;char str[500010];int map[550][550];void fun(int i,char *str){ char *p; int j=1; p=strtok(str," "); while(p!=NULL) { if(p[0]=='*') map[i][j]=-1; else map[i][j]=atoi(p); j++; p=strtok(NULL," "); }}int n,m,sx,sy,ex,ey;struct s{ int x,y,k; int step; friend bool operator <(s a,s b) { return a.step>b.step; }}a,temp;int vis[550][550];int v[550][550][4];int dirx[4]={0,1,0,-1};int diry[4]={1,0,-1,0};int jud(struct s a,int k){ if(map[a.x][a.y]==-1) return 0; if(k==1&&vis[a.x][a.y]) return 0; if(k==2&&v[a.x][a.y][a.k]) return 0; if(a.x<1||a.x>n) return 0; if(a.y<1||a.y>m) return 0; return 1;}int bfs1(){ if(sx==ex&&sy==ey) return 0; memset(vis,0,sizeof(vis)); a.x=sx; a.y=sy; a.step=map[sx][sy]; priority_queue<struct s>q; vis[sx][sy]=1; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); int i; for(i=0;i<4;i++) { temp.x=a.x+dirx[i]; temp.y=a.y+diry[i]; if(!jud(temp,1)) continue; temp.step=a.step+map[temp.x][temp.y]; if(temp.x==ex&&temp.y==ey) return temp.step; vis[temp.x][temp.y]=1; q.push(temp); } } return -1;}int bfs2(){ if(sx==ex&&sy==ey) return 0; memset(v,0,sizeof(v)); a.x=sx; a.y=sy; a.step=map[sx][sy]; int i; priority_queue<struct s>q; for(i=0;i<4;i++) { temp.x=a.x+dirx[i]; temp.y=a.y+diry[i]; temp.k=i; if(!jud(temp,2)) continue; temp.step=a.step+map[temp.x][temp.y]; if(temp.x==ex&&temp.y==ey) return temp.step; v[temp.x][temp.y][temp.k]=1; q.push(temp); } while(!q.empty()) { a=q.top(); q.pop(); for(i=0;i<4;i++) { if(i==a.k) continue; temp.x=a.x+dirx[i]; temp.y=a.y+diry[i]; temp.k=i; if(!jud(temp,2)) continue; temp.step=a.step+map[temp.x][temp.y]; if(temp.x==ex&&temp.y==ey) return temp.step; v[temp.x][temp.y][temp.k]=1; q.push(temp); } } return -1;}int main(){ int c=0; while(scanf("%d%d%d%d%d%d\n",&n,&m,&sx,&sy,&ex,&ey)!=EOF) { int i,j; for(i=1;i<=n;i++) { gets(str); fun(i,str); } /*for(i=1;i<=n;i++) { for(j=1;j<=m;j++) printf("%d ",map[i][j]); printf("\n"); }*/ int ans1=bfs1(); int ans2=bfs2(); printf("Case %d: %d %d\n",++c,ans1,ans2); }}
0 0
- NOJ 题目1642 简单的图论问题?(BFS)
- NOJ——1642简单的图论问题?(BFS+优先队列)
- NOJ 1793 简单的迷宫 (裸BFS 经典)
- BNU 1642 简单的图论问题? 最短路+BFS
- NOJ [1015] 一个简单的问题
- NBUT OJ 1642 简单的图论问题? 最短路问题,BFS
- 简单的八数码问题(BFS)
- NOJ 题目2:括号配对问题
- 基础迷宫问题-------------(图的BFS 题目取自算法竞赛入门)
- POJ 题目3984 迷宫问题(BFS)
- noj一道简单的数学题
- NOJ 题目1643 阶乘除法(暴力)
- 宽搜(BFS)经典&简单题目 - 奇怪的电梯
- NOJ(1575)-回溯算法-图的m着色问题
- NYOJ 题目58 最少步数 (简单BFS)
- 第十一届湖南省省赛 - 简单的图论问题?(BFS)
- HNNU 11657 简单的图论问题?【湖南省第十一届大学生计算机程序设计竞赛,双BFS】
- poj3278(简单的bfs)
- POJ1155 TELE
- 深入理解HTTP Session
- java引用数据类型之数组
- ipq8064 openwrt 上KGDB工作不正常
- 图片擦除-美女效果
- NOJ 题目1642 简单的图论问题?(BFS)
- Linux进程学习笔记(一)
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- 菜花这样做,就着它吃光三碗饭
- 初学Volley
- POJ 3253(Huffman树求最小权值)
- 简述java构造函数
- JAVA观感器
- NOJ 题目1645 聊天止于呵呵(模拟)