rrc12345 2022-03-18 16:14 采纳率: 83.3%
浏览 53
已结题

poj中RuntimeError无法解决

#include<cstdio>
using namespace std;
int startx,starty,tx,ty,w,h,head=0,tail=0,block;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mapp[25][25]={};
int book[25][25]={};
struct node{
    int x,y,step;
}que[626];
void bfs(int sx,int sy){
    block=1;
    for(int i=0;i<25;i++){
        for(int j=0;j<25;j++){
            book[i][j]=0;
        }
    }
    book[sx][sy]=1;
    que[tail].x=sx;
    que[tail].y=sy;
    que[tail].step=1;
    tail++;
    while(head<tail){
        for(int i=0;i<4;i++){
            tx=que[head].x+dir[i][0];
            ty=que[head].y+dir[i][1];
            if(tx>=0&&tx<h&&ty>=0&&ty<w&&book[tx][ty]==0&&mapp[tx][ty]!='#'){
                que[tail].x=tx;
                que[tail].y=ty;
                que[tail].step=que[head].step+1;
                book[tx][ty]=1;
                tail++;
                block++;
            }
        }
        head++;
    }
}
int main(){
    head=0,tail=0;
    while(scanf("%d%d",&w,&h)!=EOF){
        if(w==0&&h==0){
            break;
        }
        for(int i=0;i<25;i++){
            for(int j=0;j<25;j++){
                mapp[i][j]=0;
            }
        }
        for(int i=0;i<h;i++){
            scanf("%s",&mapp[i]);
            for(int j=0;j<w;j++){
                if(mapp[i][j]=='@'){
                    startx=i;
                    starty=j;
                }
            }
        }
        bfs(startx,starty);
        printf("%d\n",block);
    }
    return 0;
}

这段代码对应的题目:
(poj1979)
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 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)
The end of the input is indicated by a line consisting of two zeros.
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).
内容为全英文,请见谅
样例输入和样例输出被CSDN误判为无意义内容,因此此处不展示,可前往http://poj.org/problem?id=1979查看
用了G++和C++提交,都是runtime error(原因不明),请帮忙看看,谢谢

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 3月26日
    • 创建了问题 3月18日

    悬赏问题

    • ¥200 基于同花顺supermind的量化策略脚本编辑
    • ¥20 Html备忘录页面制作
    • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
    • ¥20 数学建模来解决我这个问题
    • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
    • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
    • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
    • ¥30 NIRfast软件使用指导
    • ¥20 matlab仿真问题,求功率谱密度
    • ¥15 求micropython modbus-RTU 从机的代码或库?