注意是四个方向(上下左右),不是八个方向,当成了八个方向做,一直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 1002:数塔取数问题(DP)

    1002 数塔取数问题  基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 一个高度为N的由正整数组成的三角形,从上走到下,求经过的数字和的最大值. 每 ...

  2. 《DSP using MATLAB》Problem 4.21

    快到龙抬头,居然下雪了,天空飘起了雪花,温度下降了近20°. 代码: %% -------------------------------------------------------------- ...

  3. (转)C++ 容器及选用总结

    目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭代器 第五 ...

  4. TortoiseSVN使用步骤和trunk,Branch,Tag详细说明

    1 安装及下载client 端 2 什么是SVN(Subversion)? 3 为甚么要用SVN? 4 怎么样在Windows下面建立SVN Repository? 5 建立一个Working目录 6 ...

  5. java设计模式--创建型模式(一)

    2016-04-24 10:10:34 创建型模式:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式 注意:工厂模式可以分为三类: 1)简单工厂模式(Simple Factory) 2)工厂 ...

  6. vue-cli 构建项目中 config/index.js 文件解读

    // see http://vuejs-templates.github.io/webpack for documentation. var path = require('path') module ...

  7. centos7 取消自动锁屏

    CentOS7默认短时间会锁屏,这带来了一定的麻烦,比如看电影时,你不得不时不时的动动鼠标,才能防止锁屏.在网上查了一些资料,也没有找到相关的解决办法,不过最终还是找到了. 1.打开 applicat ...

  8. centOS 6.5关闭防火墙步骤

    centOS 6.5关闭防火墙步骤 关闭命令:  service iptables stop         永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后 ...

  9. ios之mknetworkkit笔记

    asi没法用了,蛋疼了,在af和mk之间纠结,感觉af不适合我的口味,解析和网络耦合相对似乎重了点 mk似乎默认的不支持下载的断点续传,这里参考网上的代码处理了下,0修改mk的库,下面是实现的代码 / ...

  10. centos 7.5 安装mongodb

    MongoDB安装和启动 从官网下载最新对应的版本然后解压,本文以3.6.9为例,将文件拷贝到opt目录下,然后解压: [root@localhost opt]# tar zxvf mongodb-l ...