注意是四个方向(上下左右),不是八个方向,当成了八个方向做,一直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. Android Studio安装、配置教程全 - 安卓开发环境的配置手册

    Android Studio的安装.配置 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 一 Downloa ...

  2. LeetCode Single Number I II Python

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  3. RPC好,还是RESTful好?

    看到知乎上有这样一个问题 WEB开发中,使用JSON-RPC好,还是RESTful API好? 还有其他优秀的推荐方案吗? -------------------------------------- ...

  4. C语言面试题4

    第二部分:程序代码评价或者找错 1.下面的代码输出是什么,为什么?void foo(void){    unsigned int a = 6;    int b = -20;    (a+b > ...

  5. hadoop之 reduce个数控制

    1.参数变更1.x 参数名                                                         2.x 参数名 mapred.tasktracker.red ...

  6. Linux下C/C++代码调用PHP代码(转)

    Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char *  ...

  7. Windows Server 2008 R2 3389端口更改

    Windows Server 2008 R2 3389端口更改 2016-04-28 23:08 4734人阅读 评论(0) 收藏 举报  分类: Windows(61)  版权声明:本文为博主原创文 ...

  8. 复分析可视化方法:笔记:log(z)的可视化微分法

    当z转过θ时,我们来看看发生了什么: 左图中的空心箭头代表z的变化量,其长度为rδ,方向为pi/2+θ: 右图中的实心箭头代表log(z)的变化量,其长度为δ,方向为pi/2. 因此,从左图空心箭头到 ...

  9. 几个有用的Linux命令

    原文:http://spin.atomicobject.com/2013/09/09/5-unix-commands/ 本文为原文摘要. 1. man ascii 打印ascii代码表 2. cal ...

  10. 峰Spring4学习(2)依赖注入的几种方式

    一.装配一个bean 二.依赖注入的几种方式 com.cy.entity   People.java: package com.cy.entity; public class People { pri ...