HDU1312 Red and Black 题解

一条递归搜索法题目,使用递归搜索法,但是实际不用重复计算方格。

思路是:

1 每搜索一个方格就改变当前方格的值为 ‘*’,或者任何其他非‘.‘的值,代表该方格已经走过了

2 递归的时候不回复这个方格的值,就实际上不用重复搜索这个方格了,故此不用回溯

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int R, C, blacks;
vector<string> board;

inline bool isLegal(int r, int c)
{
    return r>=0 && c>=0 && r<R && c<C && board[r][c] == '.';
}

void getBlacks(int r, int c)
{
    blacks++;
    board[r][c] = '*';
    if (isLegal(r-1, c)) getBlacks(r-1, c);
    if (isLegal(r+1, c)) getBlacks(r+1, c);
    if (isLegal(r, c-1)) getBlacks(r, c-1);
    if (isLegal(r, c+1)) getBlacks(r, c+1);
}

int main()
{
    string s;
    while (cin>>C>>R && C)
    {
        board.clear();
        for (int i = 0; i < R; i++)
        {
            cin>>s;
            board.push_back(s);
        }
        blacks = 0;
        for (unsigned i = 0; i < board.size(); i++)
        {
            for (unsigned j = 0; j < board[0].size(); j++)
            {
                if ('@' == board[i][j])
                {
                    getBlacks((int)i, (int)j);
                    goto out;
                }
            }
        }
out:;
        printf("%d\n", blacks);
    }
    return 0;
}

HDU1312 Red and Black 题解

时间: 2024-11-09 00:14:46

HDU1312 Red and Black 题解的相关文章

HDU1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9732    Accepted Submission(s): 6060 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU1312——Red and Black

Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red til

Hdoj 1312.Red and Black 题解

Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can mo

《算法竞赛从入门到进阶》第四章 搜索技术 hdu1312 &quot;Red and Black&quot; BFS

#include<iostream> #include<cstdio> #include<queue> using namespace std; char room[25][25]; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; int Wx,Hy,num; //Wx,Hy:长宽的边界 ,num:从初始瓷砖到能到达的瓷砖的总数 #define CHECK(x,y) (x<Wx&&x>=0&&am

cdoj 24 8球胜负(eight) 水题

8球胜负(eight) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/24 Description 8球是一种台球竞赛的规则.台面上有7个红球.7个黄球以及一个黑球,当然还有一个白球.对于本题,我们使用如下的简化规则:红.黄两名选手轮流用白球击打各自颜色的球,如果将该颜色的7个球全部打进,则这名选手可以打黑球,如果打进则算他胜.如果在打进自己颜色的所有球之前就把黑球打进,则

小试牛刀-搜索基础入门(杭电五题)

hdu 1241 Oil Deposits 水题,BFS,判断区域的块数. 代码清单: #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef pair<int,int>P; int m,n; char s[105][105]; int xy[8][2]=

CF1276F Asterisk Substrings

抄一下 \(\color{\black}{n}\color{\red}{antf}\) 的题解 我 nantf 怎么这么强啊-这题我不到半个小时就写掉了-为什么div1的时候只有一个人做掉这个 F 啊-这个不是我 nantf 随随便便就写掉的么- 这个 CF 评分 3300 的题为什么这么水啊,直接1A了( 我们先考虑不带 star 的,人人会做,\(\sum len_i - len_{fa_i}\). 考虑 star 在最前面和最后面,最后一个字符插入到 SAM 之前统计一下 \(\sum l

HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) 解题报告

题目链接:HDU1312 / POJ1979 / ZOJ2165 Red and Black(红与黑) Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9902    Accepted Submission(s): 6158 Problem Description There is a rectangular

hdu1312(Red and Black)

点击打开杭电1312 Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles