udebug和python自生成的数据均使用无问题,仍然WA令我很困惑:求助!
查看原帖
udebug和python自生成的数据均使用无问题,仍然WA令我很困惑:求助!
452666
Elucidator_xrb楼主2021/8/3 16:24

Uva1589 代码和注释如下,希望能帮我找找问题,能找的数据都过了,能注意的细节感觉也注意了qaq

#include<stdio.h>
#include<string.h>/* memset在string.h中 */
#include<stdlib.h>
//#define LOCAL

typedef struct pl{
    char type[2];
    int x,y;
} Coord;/* 棋子对象 */
Coord bG,rPiece[7];/* bG黑方将;rPiece红方棋子 */
int N; /* 记录rPiece数量 */
int ocp[11][10]; /* 记录棋子物理占用 */
int chmap[11][10]; /* 记录安全地,0为安全,1为被杀 */

int isInPalace(int x,int y); /* (x,y)在城堡内返回1 */
int isInBoard(int x,int y);  /* (x,y)在城堡内返回1*/
int calAttempt(const int X,const int Y); /* 计算逃至(X,Y)位置能否存活,能存活返回1 */
int isSafe(int x,int y); /* 总的包装函数,判断黑方回合的行动能存活返回1 */

int main(){

#ifdef LOCAL
    freopen("data1.in","r",stdin);
    freopen("data.out","w",stdout);
#endif

    while(scanf("%d%d%d",&N,&bG.x,&bG.y)==3 && N){
        for(int i=0;i<11;++i) { /* 初始化两个地图 */
            memset(ocp[i],0,10*sizeof(int));
            memset(chmap[i],0,10*sizeof(int));
        }

        for(int i=0;i<N;++i) { /* 读入红方棋子 */
            scanf("%s%d%d",rPiece[i].type,&rPiece[i].x,&rPiece[i].y);
            ocp[rPiece[i].x][rPiece[i].y]=1;
        }

        if(isSafe(bG.x,bG.y)) printf("NO\n");
        else printf("YES\n");        
    }
    return 0;
}

int isInPalace(int x,int y){  return 1<=x && x<=3 && 4<=y && y<=6; }
int isInBoard(int x,int y) { return 1<=x && x<=10 && 1<=y && y<=9; }

int isSafe(int x,int y){
    int flag=0;
/* 致命疏忽,忘考虑使用“飞将”将军的傻逼走法:此时黑方直接反杀获胜 */
    for(int k=0;k<N;++k){
        if(rPiece[k].type[0]=='G' && rPiece[k].y==y){
            int i;
            for(i=x+1;ocp[i][y]==0;++i);
            if(i==rPiece[k].x) return 1;
        }
    }
    if(isInPalace(x-1,y) && calAttempt(x-1,y)) return 1;/* 模拟4种走位情况 */
    if(isInPalace(x+1,y) && calAttempt(x+1,y)) return 1;
    if(isInPalace(x,y-1) && calAttempt(x,y-1)) return 1;
    if(isInPalace(x,y+1) && calAttempt(x,y+1)) return 1;
    return 0;
}

int calAttempt(const int X,const int Y){
    for(int i=0;i<N;++i){/* 检查黑方能否吃棋 */
        if(X==rPiece[i].x && Y==rPiece[i].y){
            ocp[X][Y]=0;
            continue;
        }
    }
    for(int k=0;k<N;++k){/* 开始模拟计算红方所有棋子的打击范围 */
        int x=rPiece[k].x,y=rPiece[k].y;
        if(X==x && Y==y) continue;
        char t=rPiece[k].type[0];
        if(t=='G' || t=='R'){
            for(int i=x-1;i>=1 && ocp[i][y]==0;--i) chmap[i][y]=1;
            for(int i=x+1;i<=10 && ocp[i][y]==0;++i) chmap[i][y]=1;
            for(int i=y-1;i>=1 && ocp[x][i]==0;--i) chmap[x][i]=1;
            for(int i=y+1;i<=9 && ocp[x][i]==0;++i) chmap[x][i]=1;
        }
        else if(t=='C'){
            int i;  /* 这里i误打成x竟然歪打正着过了udebug所有样例 */
            for(i=x-1;i>=1 && ocp[i][y]==0;--i);
                for(int j=i-1;j>=1 && ocp[j][y]==0;--j) chmap[j][y]=1;
            for(i=x+1;i<=10 && ocp[i][y]==0;++i);
                for(int j=i+1;j<=10 && ocp[j][y]==0;++j) chmap[j][y]=1;
            for(i=y-1;i>=1 && ocp[x][i]==0;--i);
                for(int j=i-1;j>=1 && ocp[x][j]==0;--j) chmap[x][j]=1;
            for(i=y+1;i<=9 && ocp[x][i]==0;++i);
                for(int j=i+1;j<=9 && ocp[x][j]==0;++j) chmap[x][j]=1; 
        }
        else if(t=='H'){
            if(isInBoard(x+1,y) && ocp[x+1][y]==0){
                if(isInBoard(x+2,y+1) && ocp[x+2][y+1]==0) chmap[x+2][y+1]=1;
                if(isInBoard(x+2,y-1) && ocp[x+2][y-1]==0) chmap[x+2][y-1]=1;
            }
            if(isInBoard(x-1,y) && ocp[x-1][y]==0){
                if(isInBoard(x-2,y+1) && ocp[x-2][y+1]==0) chmap[x-2][y+1]=1;
                if(isInBoard(x-2,y-1) && ocp[x-2][y-1]==0) chmap[x-2][y-1]=1;
            }
            if(isInBoard(x,y+1) && ocp[x][y+1]==0){
                if(isInBoard(x+1,y+2) && ocp[x+1][y+2]==0) chmap[x+1][y+2]=1;
                if(isInBoard(x-1,y+2) && ocp[x-1][y+2]==0) chmap[x-1][y+2]=1;
            }
            if(isInBoard(x,y-1) && ocp[x][y-1]==0){
                if(isInBoard(x+1,y-2) && ocp[x+1][y-2]==0) chmap[x+1][y-2]=1;
                if(isInBoard(x-1,y-2) && ocp[x-1][y-2]==0) chmap[x-1][y-2]=1;
            }
        }
    }
    if(chmap[X][Y]==1) return 0;
    else return 1;
}

使用了别人的python代码自生成数据

import random
import sys
 
s = ["H", "C", "R"]
file = open('data1.in', 'w')
for i in range(500):
    l = []
    n  = random.randint(2,7)
    b_x = random.randint(1,3)
    b_y = random.randint(4,6)
    l.append((b_x,b_y))
    t_x = random.randint(8, 10)
    t_y = random.randint(4, 6)
    l.append((t_x, t_y))
    while (len(l)<9):
        h_x = random.randint(1, 10)
        h_y = random.randint(1, 9)
        if((h_x,h_y) in l):
            continue
        l.append((h_x, h_y))
    x = []
    t = [0,0,0]
    x.append(0)
    x.append(1)
    file.write(str(n)+" "+str(b_x)+" "+str(b_y)+'\n')
    file.write("G "+str(t_x)+" "+str(t_y)+'\n')
    d = 1
    while(d<n):
        now = random.randint(0,2)
        if(t[now]>=2):
            continue
        now_2 = random.randint(2,8)
        if(now_2 in x):
            continue
        x.append(now_2)
        t[now] += 1
        file.write(s[now] + " " + str(l[now_2][0]) + " " + str(l[now_2][1]))
        file.write('\n')
        d+=1
    file.write('\n')
file.write('0 0 0\n')
file.close()
2021/8/3 16:24
加载中...