uva-657-搜索
注意是四个方向(上下左右),不是八个方向,当成了八个方向做,一直wa
AC时间:0ms
#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
struct DIR
{
int r;
int c;
char cc;
};
const DIR dir[] = { {-1,0},{1,0},{0,-1},{0,1}};
const int MAXR = 60; void markX(queue<DIR> nq, queue<DIR> &q, char a[][MAXR], int r, int c,
int used[][MAXR])
{
DIR d;
while (!nq.empty())
{
d = nq.front();
nq.pop();
for (int i = 0; i < 4; i++)
{
int nr = d.r + dir[i].r;
int nc = d.c + dir[i].c;
if (nr < 0 || nc < 0 || nr == r || nc == c || used[nr][nc] == 1
|| a[nr][nc] == '.')
continue;
used[nr][nc] = 1;
DIR ddd;
ddd.r = nr;
ddd.c = nc;
ddd.cc = a[nr][nc];
if (ddd.cc == '*')
{
q.push(ddd);
}
else
nq.push(ddd);
}
}
} void bfs(queue<DIR> q, char a[][MAXR], int r, int c, int used[][MAXR],
int* total)
{
while (!q.empty())
{
DIR dd = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nr = dd.r + dir[i].r;
int nc = dd.c + dir[i].c;
if (nr < 0 || nc < 0 || nr == r || nc == c || used[nr][nc] == 1
|| a[nr][nc] == '.')
continue;
used[nr][nc] = 1;
DIR ddd;
ddd.r = nr;
ddd.c = nc;
ddd.cc = a[nr][nc];
if (ddd.cc == '*')
q.push(ddd);
else
{
queue<DIR> q2;
q2.push(ddd);
markX(q2, q, a, r, c, used);
if(dd.cc=='*')
(*total)++;
}
}
}
}
int main()
{ int h, w;
int tc = 0;
while (cin >> w >> h)
{
tc++;
if (h == w && h == 0)
return 0;
cout << "Throw " << tc << endl;
char a[MAXR][MAXR];
int used[MAXR][MAXR];
memset(a, '.', sizeof(a));
memset(used, 0, sizeof(used));
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> a[i][j];
int t[20];
memset(t, 0, sizeof(t));
int length = 0;
queue<DIR> q1;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (used[i][j] == 0 && a[i][j] == 'X')
{
DIR dd;
dd.r = i;
dd.c = j;
dd.cc = a[i][j];
used[i][j] = 1;
q1.push(dd);
int total = 1;
bfs(q1, a, h, w, used, &total);
t[length++] = total;
}
for (int i = 0; i < length; i++)
for (int j = 1; j < length; j++)
if (t[j - 1] > t[j])
{
int k = t[j - 1];
t[j - 1] = t[j];
t[j] = k;
}
for (int i = 0; i < length; i++)
if (i == 0)
cout << t[i];
else
cout << " " << t[i];
cout << endl<<endl;;
}
return 0;
}
uva-657-搜索的更多相关文章
- UVA 657 The die is cast
The die is cast InterGames is a high-tech startup company that specializes in developing technolo ...
- UVa 657 掷骰子
意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...
- uva 657
很简单的题,就是题意不懂……! 就是判断每个'*'区域内‘X’区域块的个数 WA了好多次,就是太差了: 1.结果排序输出 2.因为是骰子所以不再1-6范围内的数字要舍弃 3.格式要求要空一行…… 4. ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- UVA 707 - Robbery(内存搜索)
UVA 707 - Robbery 题目链接 题意:在一个w * h的图上.t个时刻,然后知道一些信息,每一个时刻没有小偷的矩阵位置,问哪些时刻能够唯一确定小偷位置.和确定小偷是否已经逃走,假设没逃走 ...
- UVA - 10118Free Candies(记忆化搜索)
题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- UVA.129 Krypton Factor (搜索+暴力)
UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...
随机推荐
- [LeetCode&Python] Problem 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- iPhone/iPad被停用怎么办 3招轻松解锁
家中小孩玩电脑游戏,自己拿了iPad,随便输入密码,结果造成平板电脑无法使用,相信这是许多家长都将面对或是早已发生的事情.本文整理当iPhone或是iPad被停用时的处理方法. iPhone被停用 为 ...
- (4)格式化输出(%用法和format用法以及区别)
%s用法(%s的用法是写多少个,后面就要传多少个) format用法(基本语法是通过{}和:来代替%.format函数可以接受不限个参数,位置可以不按顺序) 形式一(顺序填坑{}) >>& ...
- Thinkphp分页类使用
3.2.2分页设置变化: $count = $pro->count(); //查询出总条数 $page=new \Think\Page($count,5); $page->rollPage ...
- Struts2重新学习之自定义拦截器(判断用户是否是登录状态)
拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...
- smarty学习——变量调节器(过滤器)
变量调节器用于变量,自定义函数和字符串. 请使用 | 符号和调节器名称应用调节器.变量调节器由赋予的参数值决定其行为.参数由:符号分开. 比如进行大写转换的: upper demo: <br&g ...
- php过滤html标签截取部分内容
<?php $str = '<span>fdsfsdf</span><a href="#">href</a>'; echo h ...
- actor model vs tasked based parallizm
举例子:计算pi actor model概念:一般有n个actor(task),和一个调度线程(本身也是一个actor)调度线程负责向每个task发送命令执行计算,以及接收每个task的结果并归并到一 ...
- Oracle误删除表空间的恢复
对于误删除表空间的恢复,本文通过基于数据库的时间点恢复和基于表空间的时间点恢复分别加以讨论 一 通过基于数据库的时间点恢复被误删除的表空间 1 需要注意的事项 a 基于数据库的时间点恢复将会回退整个数 ...
- webpack externals
当我们想在项目中require一些其他的类库或者API,而又不想让这些类库的源码被构建到运行时文件中,这在实际开发中很有必要.此时我们就可以通过配置externals参数来解决这个问题: //webp ...