1012 Rescue
2016-04-18 22:55
381 查看
题意:
X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间
思路:
BFS 问题 很普通的一个问题 用优先队列解决
求出所有的情况 重载运算符‘<’将所有情况放于优先队列中 输出最小的
X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间
思路:
BFS 问题 很普通的一个问题 用优先队列解决
求出所有的情况 重载运算符‘<’将所有情况放于优先队列中 输出最小的
// ConsoleApplication17.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<fstream> #include<iostream> #include <stdio.h> #include <string.h> #include <queue> using namespace std; struct node { int x, y, step; friend bool operator<(node n1, node n2) { return n2.step<n1.step; } }; int n, m, visit[205][205]; char map[205][205]; int sx, x2, sy, y2; int to[4][2] = { 1,0,-1,0,0,1,0,-1 }; int check(int x, int y) { if (x<0 || y<0 || x >= n || y >= m || !visit[x][y] || map[x][y] == '#') return 1; return 0; } int bfs() { int i; priority_queue<node> Q; node a, next; a.x = sx; a.y = sy; a.step = 0; Q.push(a); visit[sx][sy] = 0; while (!Q.empty()) { a = Q.top(); Q.pop(); if (a.x == x2 && a.y == y2) return a.step; for (i = 0; i<4; i++) { next = a; next.x += to[i][0]; next.y += to[i][1]; if (check(next.x, next.y)) continue; next.step++; if (map[next.x][next.y] == 'x') next.step++; if (visit[next.x][next.y] >= next.step) { visit[next.x][next.y] = next.step; Q.push(next); } } } return 0; } int main() { fstream cin("E:/C++/IN/aaa.txt"); int i, j; while (cin>>n>>m) { for (i = 0; i < n; i++) for (j = 0;j < m;j++) cin >> map[i][j]; for (int i = 0;i < n;i++) for (j = 0; map[i][j]; j++) { if (map[i][j] == 'r') { sx = i; sy = j; } else if (map[i][j] == 'a') { x2 = i; y2 = j; } } } memset(visit, 1, sizeof(visit)); int ans = 0; ans = bfs(); if (ans) cout<<ans<<endl; else cout << "Poor ANGEL has to stay in the prison all his life." << endl;; return 0; }
相关文章推荐
- Qt MinGW 使用联合编译IncrediBuild
- Github Coding Developer Book For LiuGuiLinAndroid
- Github Coding Developer Book For LiuGuiLinAndroid
- 2016-NJUST-sequence
- Ubitquity LLC用区块链保障房…
- iOS UITableView的cell重用标识
- POJ 2785 4 Values whose Sum is 0 折半枚举(双向搜索)
- APUE------文件I/O
- IOS开发-UIView之动画效果的实现方法(合集)
- Android-Bluetooth Low Energy(BLE)
- error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
- 关于getting 'android:label' attribute: attribute is not a string value 错误的解决方法
- 深入源码解析Android中的Handler,Message,MessageQueue,Looper
- Android UI---自定义形状shape
- .NET Business Connector mode assembly is built against version 'v2.0.50727' of the runtime
- 基于KVM的Mirantis Fuel 8.0环境搭建
- 学习笔记(三)UI开发之一
- LinkedBlockingQueue(lbq)阻塞队列
- request和response
- leetcode 63. Unique Paths II