【HDOJ】3316 Mine sweeping
简单BFS。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 105 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; char map[MAXN][MAXN];
char ans[MAXN][MAXN];
bool visit[MAXN][MAXN];
int dir[][] = {
{-,},{,},{,},{,-},
{,},{,-},{-,},{-,-}
};
node_t s;
int n; bool check(int x, int y) {
return x< || x>=n || y< || y>=n;
} void bfs() {
int i, j, k, tmp;
int x, y, v;
int xx[], yy[], m;
queue<node_t> Q;
node_t nd; memset(visit, false, sizeof(visit));
memset(ans, '.', sizeof(ans));
visit[s.x][s.y] = true;
for (i=; i<n; ++i)
ans[i][n] = '\0';
Q.push(s); while (!Q.empty()) {
nd = Q.front();
Q.pop();
v = m = ;
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y))
continue;
xx[m] = x;
yy[m] = y;
++m;
if (map[x][y] == 'X')
++v;
}
ans[nd.x][nd.y] = v+'';
if (v == ) {
for (i=; i<m; ++i) {
if (!visit[xx[i]][yy[i]]) {
visit[xx[i]][yy[i]] = true;
Q.push(node_t(xx[i], yy[i]));
}
}
}
}
} int main() {
int i, j; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d", &n) != EOF) {
for (i=; i<n; ++i)
scanf("%s", map[i]);
scanf("%d %d", &s.x, &s.y); if (map[s.x][s.y] == 'X') {
printf("it is a beiju!\n\n");
} else {
bfs();
for (i=; i<n; ++i)
printf("%s\n", ans[i]);
printf("\n");
}
} return ;
}
【HDOJ】3316 Mine sweeping的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】4341 Gold miner
分组01背包.在一条直线上的点归为一组. /* 4341 */ #include <iostream> #include <sstream> #include <stri ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- SKView类
继承自 UIView:UIResponder:NSObject 符合 NSCoding(UIView)UIAppearance(UIView)UIAppearanceContainer(UIView) ...
- 配置keil MDK和keil C51共存
配置keil MDK和keil C51共存:1.首先安装keilMDK或者安装KeilC51其中一个:2.安装到D:\keil路径下,按照默认的配置安装,完成:3.使用管理员身份打开安装好的软件,打开 ...
- 手工释放Linux内存
转载自:http://blog.csdn.net/wyzxg/article/details/7279986/ linux的内存查看: [root@localhost 0.1.0]# free -m ...
- JavaScript 浮点数运算 精度问题
JavaScript小数在做四则运算时,精度会丢失,这会在项目中引起诸多不便,先请看下面脚本. //加减 <script type="text/javascript" lan ...
- hdu 2187
#include <stdio.h> using namespace std; struct race { int p; int w; }; race r[1010]; int cmp(r ...
- canvas toDataUrl 跨域问题
使用canvas 的 toDataUrl方法会遇到跨域问题 chrome 会报下面的错误: Uncaught SecurityError: Failed to execute 'toDataURL' ...
- 数据库、C#、Java生成唯一GUID 方法
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值.GUID ...
- mysql数据库编码
MySQL数据库默认的编码是: character set :latin1 collation : latin1_swedish_ci 查看MySQL支持的编码: mysql> show cha ...
- MD5加密运算
//MD5 对字符串的加密 -(void)demo1 { NSString *str = @"love"; //对字符串进行MD5加密 str = str.md5String; N ...
- new date() 函数在浏览器中的兼容问题!!
引言: 同一种语言javascript,在不同的浏览器中,存在语言兼容性问题,本质上是由于不同的浏览器是支持的语言标准和实现上各有差异.本文将基于new Date来创建Date对象来分析这个问题. v ...