2021.01.28刷题总结

H - Red and Black

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 move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#





#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.

11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13

这题是个经典的深度优先搜索题目,在 《挑战程序算法竞赛》《啊哈!算法》 两本书都有提到过。
解题思路:
首先我们找到到 ‘@’的坐标,然后往 ‘@’的上下左右四个方向走就行了
记得把走过的地方改成‘#’,这样就可以避免重复走
在这里插入图片描述
如图,绿块是我们可以走的地方,然后再以两个绿块为中心,继续向四个方向走
在这里插入图片描述
AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 25;
char m[maxn][maxn];
int ans = 0;
int w, h;
void solove(int x, int y)
{
    m[y][x] = '#';
    int nx, ny;
    for (int i = -1; i <= 1; i++) //偏移x轴
    {
        nx = x + i, ny = y;
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == '.')
        {
            // cout << "(ny,nx):" << ny << ' ' << nx << ':';
            // cout << ++ans << endl;
            ++ans;
            solove(nx, ny);
        }
    }
    for (int i = -1; i <= 1; i++) //偏移y轴
    {
        nx = x, ny = y + i;
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == '.')
        {
            // cout << "(ny,nx):" << ny << ' ' << nx << ':';
            // cout << ++ans << endl;
            ++ans;
            solove(nx, ny);
        }
    }
}
int main(void)
{
    int x, y;
    while (cin >> w >> h)
    {
        if (w <= 0 && h <= 0)
            break;
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++)
            {
                cin >> m[i][j];
                if (m[i][j] == '@')
                    x = j, y = i;
            }
        ans = 1;
        //cout << x << y;
        solove(x, y);
        cout << ans << endl;
    }
    return 0;
}

F - Catch That Cow (还未解决)

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
*Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
*Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

先来看一下我的第一种错误解法:
分成两种情况:
一、在牛的左边:我就把三种情况都做一遍:向左走,向右走,传送,然后比较谁最小
二、如果在牛的右边,我只需要向左走就行了

其实这个思路应该没问题,可是代码实现出了问题:

int solove(int i)
{
    if (i < 0)
        return 100000;
    if (i == k)
        return 0;
    if (i < k)
    {
        int a = solove(2 * i) + 1, b = solove(i + 1) + 1, c = solove(i - 1) + 1;
        return min(min(a, b), c);
    }
    return solove(i - 1) + 1;
}

因为执行 solove(i-1) 之后,又会执行 solove(i+1) ,一加一减抵消,形成死循环

然后是第二种错误写法,这个用到了 广搜 的思想:

#include <iostream>
#include <queue>
using namespace std;
int n, k;
const int maxn = 110000;
struct node
{
    int pos, step;
};
bool visited[maxn] = {false};
int main(void)
{
    cin >> n >> k;
    queue<node> que;
    node front = {n, 0};
    node temp;
    visited[front.pos] = true;
    que.push(front);
    while (que.size())
    {
        front = que.front();
        que.pop();
        向后走一步
        temp = front;
        temp.pos--, temp.step++; 
        if (temp.pos == k)
            break;
        if (temp.pos >= 0 && visited[temp.pos] == false) //如果该点没有来过,且位置不能越界
        {
            que.push(temp);           //入队
            visited[temp.pos] = true; //该点已经来过
        }
        //向前走一步
        if (front.pos < k)
        {
            temp = front;
            temp.pos++, temp.step++; 
            if (temp.pos == k)
                break;
            if (temp.pos < maxn && visited[temp.pos] == false) //如果该点没有来过,且位置不能越界
            {
                que.push(temp);           //入队
                visited[temp.pos] = true; //该点已经来过
            }
            //传送
            temp = front;
            temp.pos *= 2, temp.step++; //
            if (temp.pos == k)
                break;
            if (temp.pos < maxn && visited[temp.pos] == false) //如果该点没有来过,且位置不能越界
            {
                que.push(temp);           //入队
                visited[temp.pos] = true; //该点已经来过
            }
        }
    }
    cout << temp.step << endl;
    return 0;
}

刚开没有考虑到重复访问,导致做了很多重复操作,然后用了个visited记录访问状态,实现树的剪枝
然后没考虑到越界的情况,后来加上去了。
然后还是 WA,目前没找出来错误… …

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值