bfs()。题目的数据乱码。应该如下:

*****#*********
*.......$...*
*..***.......*
*....*****..*
*....******37A
*****......*
*.....******..*
***CA********** *****
*$**
*.**
***#* --
 #include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; #define isUpper(ch) (ch>='A' && ch<='Z')
#define isLower(ch) (ch>='a' && ch<='z')
#define isDigit(ch) (ch>='0' && ch<='9')
#define INF 0x7f7f7f7f
#define MAXN 105 typedef struct node_t {
int x, y, z, t;
node_t() {}
node_t(int xx,int yy, int zz, int tt) {
x = xx; y = yy; z = zz; t = tt;
}
friend bool operator < (const node_t a, const node_t b) {
return a.t > b.t;
}
} node_t; int visit[MAXN][MAXN][];
char map[MAXN][MAXN];
int m, n;
int dir[][] = {-,,,,,-,,}; bool check(int x, int y) {
return x<||x>=m||y<||y>=n;
} int bfs() {
int x, y, z, t;
int i, j;
priority_queue<node_t> Q; memset(visit, 0x7f, sizeof(visit));
for (i=; i<m; ++i) {
for (j=; j<n; ++j) {
if (map[i][j]=='#') {
Q.push(node_t(i, j, , ));
visit[i][j][] = ;
} else if (isUpper(map[i][j])) {
Q.push(node_t(i, j, map[i][j]-'A'+, ));
visit[i][j][map[i][j]-'A'+] = ;
}
}
} while (!Q.empty()) {
node_t nd = Q.top(); if (map[nd.x][nd.y] == '$')
return nd.t; Q.pop();
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y) || map[x][y]=='*')
continue;
if (map[x][y] == '.') {
if (nd.t < visit[x][y][nd.z]) {
visit[x][y][nd.z] = nd.t;
Q.push(node_t(x, y, nd.z, nd.t));
}
} else if (map[x][y] == '$') {
Q.push(node_t(x, y, nd.z, nd.t));
} else if (isDigit(map[x][y])) {
if (nd.z > && nd.t < visit[x][y][nd.z-]) {
visit[x][y][nd.z-] = nd.t;
Q.push(node_t(x, y, nd.z-, nd.t));
}
t = nd.t+map[x][y]-'';
if (t < visit[x][y][nd.z]) {
visit[x][y][nd.z] = t;
Q.push(node_t(x, y, nd.z, t));
}
}
}
} return -;
} int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif int i = , tmp; while (gets(map[i++]) != NULL) {
if (map[][] == '-')
break;
while (gets(map[i])!=NULL && strlen(map[i])!=)
++i;
m = i;
n = strlen(map[]);
tmp = bfs();
if (tmp == -)
printf("IMPOSSIBLE\n");
else
printf("%d\n", tmp);
i = ;
} return ;
}

【HDOJ】2416 Treasure of the Chimp Island的更多相关文章

  1. Treasure of the Chimp Island

    Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  2. 快速切题 hdu2416 Treasure of the Chimp Island 搜索 解题报告

    Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. 【HDOJ】5446 Unknown Treasure

    1. 题目描述题目很简单,就是求$C(n,m) % M$. 2. 基本思路这是一道应用了众多初等数论定理的题目,因为数据范围较大因此使用Lucas求$C(n,m) % P$.而M较大,因此通过$a[i ...

  4. 【HDOJ】1448 The Treasure

    这就是个简单的bfs.真没什么好说的,三维的状态就可以了.每次预处理一下monster的位置,然后再恢复. /* 1924 */ #include <iostream> #include ...

  5. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  6. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  7. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  8. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  9. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

随机推荐

  1. 深入理解javascript闭包(一)

    闭包(closure)是Javascript语言的一个难点.也是它的特色,非常多高级应用都要依靠闭包实现. 一.什么是闭包? 官方"的解释是:闭包是一个拥有很多变量和绑定了这些变量的环境的表 ...

  2. ios中xib的使用介绍

    ios中Xib的使用 ios中xib的使用 Nib files are the quintessential(典型的) resource type used to create iOS and Mac ...

  3. (转)Android中使用ormlite实现持久化(一)--HelloOrmLite

    Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主 要是我对sql语言不熟悉).而Java Web开发中有很多orm框架,但 ...

  4. POJ 1696 Space Ant 卷包裹法

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3316   Accepted: 2118 Descrip ...

  5. .net验证控件

    一.客户端验证(用户体验,减少服务器端压力) 二.服务器端验证(防止恶意攻击,客户端js很容易被绕过) 验证控件:RequiredFieldValidator:字段必填:RangeValidator: ...

  6. oracle 添加自增索引

    1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...

  7. Linq101-Quantifiers

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Quantif ...

  8. HTML5 Canvas 概述

    本文中,我们将探索如何使用HTML5的Canvas API.Canvas API很酷,我们可以通过它来动态创建生成和展示图形,图表,图像以及动画.本文将使用渲染API(rendering API)的基 ...

  9. 7——使用TextView实现跑马灯

    首先给TextView添加一个单行限制: android:singleLine="true" - 解决方案一 更改TextView的一个属性: android:ellipsize= ...

  10. WearableListView的使用和一些思考

    今年加盟了一家做手表的公司,至此开启了androidwear(类)的开发之门. 近日要做一个手表上的List显示,为此也是花了很多的心思在List效果上,多日下来,有些心得. 一.需求确定: 手表上的 ...