注意是四个方向(上下左右),不是八个方向,当成了八个方向做,一直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-搜索的更多相关文章

  1. UVA 657 The die is cast

      The die is cast  InterGames is a high-tech startup company that specializes in developing technolo ...

  2. UVa 657 掷骰子

    意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...

  3. uva 657

    很简单的题,就是题意不懂……! 就是判断每个'*'区域内‘X’区域块的个数 WA了好多次,就是太差了: 1.结果排序输出 2.因为是骰子所以不再1-6范围内的数字要舍弃 3.格式要求要空一行…… 4. ...

  4. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  5. UVA 707 - Robbery(内存搜索)

    UVA 707 - Robbery 题目链接 题意:在一个w * h的图上.t个时刻,然后知道一些信息,每一个时刻没有小偷的矩阵位置,问哪些时刻能够唯一确定小偷位置.和确定小偷是否已经逃走,假设没逃走 ...

  6. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  7. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  8. 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 ...

  9. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  10. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

随机推荐

  1. 51Nod:1003 阶乘后面0的数量

    1003 阶乘后面0的数量  基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 n的阶乘后面有多少个0? 6的阶乘 = 1*2*3*4*5*6 = 72 ...

  2. ClickHouse 简单试用

    ClickHouse 具有强劲的数据分析能力,同时支持标准sql 查询,内置了好多聚合参数 同时可以方便的使用表函数连接不同的数据源(url,jdbc,文件目录...) 测试使用docker安装 参考 ...

  3. ThinkPHP 5 中的 composer.json

    本篇并不是揭 ThinkPHP 5 的问题. 只是通过 composer.json 来学习 compoer.json 元旦那天, ThinkPHP 5.1 正式发布,值得庆祝. 之后的第二天有人反馈 ...

  4. 【转】每天一个linux命令(56):netstat命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...

  5. meta viewport 理解

    移动设备上的浏览器如果不指明 viewport 这个meta,当你从移动设备上浏览网页的时候,它假设(你浏览的是桌面版并且你想看到所有的内容),不只是一个左上角.因此,它会把viewport的宽度设置 ...

  6. jquery ajax 上传文件

    html:<!-- /.tab-pane --> <div class="tab-pane" id="head_portrait"> & ...

  7. 使用js提交form表单的两种方法

    提交form表单的时候瑶族一些简单的验证,验证完后才能提交,避免无效提交. 1.当输入用户名和密码为空的时候,需要判断.这时候就用到了校验用户名和密码,这个需要在前端页面写:有两种方法,一种是用sub ...

  8. php实现Facebook风格的 time ago函数

    php实现Facebook风格的 time ago函数 非常好用,只要把里面的英文替换成中文就行了 英文函数代码如下: <?php function nicetime($date) { if(e ...

  9. POJ1179 Polygon

    题目:http://poj.org/problem?id=1179 石子合并的升级版.有负值.但运算符只有 + 和 * . 考虑负值对原做法正确性的影响:之所以仅记录最大值可能不对,是因为有可能负数 ...

  10. Spring Cloud 入门 之 Config 篇(六)

    原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分 ...