编程介的小学生 2019-12-30 21:50 采纳率: 0.2%
浏览 78

Roll The Cube C语言

Problem Description
This is a simple game.The goal of the game is to roll two balls to two holes each.
'B' -- ball
'H' -- hole
'.' -- land
'*' -- wall
Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'.
Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up.
A ball will stay where it is if its next point is a wall, and balls can't be overlap.
Your code should give the minimun times you press the keys to achieve the goal.

Input
First there's an integer T(T<=100) indicating the case number.
Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map.
Then n lines each with m characters.
There'll always be two balls(B) and two holes(H) in a map.
The boundary of the map is always walls(*).

Output
The minimum times you press to achieve the goal.
Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.

Sample Input
4
6 3


B
B
H
H


4 4


BB
HH


4 4


BH
HB


5 6


.BB*
.H*H
...*


Sample Output
3
1
2
Sorry , sir , my poor program fails to get an answer.
Roll The Cube

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 15:34
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    #include <stdio.h>
    int main() {
        int t;
        scanf("%d", &t);
        while(t--) {
            int n, m;
            scanf("%d%d", &n, &m);
            char grid[n][m];
            for(int i = 0; i < n; ++i) {
                scanf(" %s", grid[i]);
            }
            int x[4] = {0, 0, -1, 1}, y[4] = {-1, 1, 0, 0};
            int res = 0x7fffffff;
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < m; ++j) {
                    int count = 0;
                    if(grid[i][j] == 'B') {
                        for(int k = 0; k < 4; ++k) {
                            int nx = i + x[k], ny = j + y[k];
                            if(nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != '*') {
                                ++count;
                            }
                        }
                        if(count == 2) {
                            res = min(res, count + 1);
                        }
                    }
                }
            }
            printf("%d\n", res);
        }
        return 0;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集