UVA 1589 求問這個題到底哪里有bug 我一直wa,所以才想求救大家的,希望大家幫幫我


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
char board[11][10];
struct chess
{
    char name;
    int a;
    int b;
}c[8];//代表的是7個棋子
int Kill(int x,int y,int n)
{
    for(int i=1;i<=n;i++)//逐個遍歷是否有威脅,有威脅,winflag++
            {
                if(c[i].name=='G')//將軍
                {
                     if(c[i].b==y)//在同一列
                    {
                        int flag=0;
                        for(int j=x+1;j<=c[i].a-1;j++)
                        {
                            if(board[j][y]!='\0')
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag==0)
                            return 1;
                    }
                    else
                        continue;
                }
                else if(c[i].name=='C')//火炮,搜索是否與(x-1,y)在同一條直線上,如果在的話,看其中是否只隔了一個棋子
                {
                    if(y==c[i].b)
                    {
                        int flag=0;
                        int min1,max1;
                        min1=x<c[i].a?x:c[i].a;
                        max1=x>c[i].a?x:c[i].a;
                        for(int j=min1+1;j<=max1-1;j++)
                        {
                            if(board[j][y]!='\0')//統計中間棋子的數量
                            {
                                flag++;
                            }
                        }
                        if(flag==1)
                            return 1;

                    }
                    else if(x==c[i].a)
                    {
                        int flag=0;//比較炮和將的位置
                        int min1,max1;
                        min1=y<c[i].b?y:c[i].b;
                        max1=y>c[i].b?y:c[i].b;
                        for(int j=min1+1;j<=max1-1;j++)
                        {
                            if(board[x][j]!='\0')//統計中間棋子的數量
                            {
                                flag++;
                            }
                        }
                        if(flag==1)
                            return 1;
                    }
                    else
                        continue;

                }
                else if(c[i].name=='R')//只要其中沒有任何的棋子
                {
                    if(y==c[i].b)
                    {
                        int flag=0;
                        int min1,max1;
                        min1=x<c[i].a?x:c[i].a;
                        max1=x>c[i].a?x:c[i].a;
                        for(int j=min1+1;j<=max1-1;j++)
                        {
                            if(board[j][y]!='\0')//統計中間棋子的數量
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag==0)
                            return 1;
                    }
                    else if(x==c[i].a)
                    {
                        int flag=0;
                        int min1,max1;
                        min1=y<c[i].b?y:c[i].b;
                        max1=y>c[i].b?y:c[i].b;
                        for(int j=min1+1;j<=max1-1;j++)
                        {
                            if(board[x][j]!='\0')//統計中間棋子的數量
                            {
                                flag=1;
                                break;
                            }
                        }
                        if(flag==0)
                            return 1;
                    }
                    else
                        continue;

                }
                else if(c[i].name=='H')//馬
                {
                    if(abs(c[i].b-y)==1&&c[i].a-x==-2&&board[c[i].a+1][c[i].b]=='\0')
                        return 1;
                    else if(abs(c[i].b-y)==1&&c[i].a-x==2&&board[c[i].a-1][c[i].b]=='\0')
                        return 1;
                    else if(abs(c[i].a-x)==1&&c[i].b-y==2&&board[c[i].a][c[i].b-1]=='\0')
                        return 1;
                    else if(abs(c[i].a-x)==1&&c[i].b-y==-2&&board[c[i].a][c[i].b+1]=='\0')
                        return 1;
                    else
                        continue;
                }
            }
    return 0;
}
int main()
{
    struct chess exc;
    int n,x,y;
    char cx;
    while(scanf("%d%d%d",&n,&x,&y)==3&&n&&x&&y)
    {
        scanf("%c",&cx);
        memset(board,'\0',sizeof(board));
        c[0].name='Q';
        c[0].a=x;
        c[0].b=y;
        board[x][y]='Q';//代表的黑方的將
        for(int i=1;i<=n;i++)//輸入,初始化棋盤
        {
            scanf("%c%d%d",&c[i].name,&c[i].a,&c[i].b);
            scanf("%c",&cx);
            board[c[i].a][c[i].b]=c[i].name;
        }
         int winflag=0;//四個 方向都不能走,則為4,此時輸出yes
        //如果兩個人直接見面了,那么就直接輸出no
        for(int i=1;i<=n;i++)
        {
            if(c[i].name=='G')
            {
                if(y==c[i].b)
                {
                    int flag=0;
                    for(int j=x+1;j<=c[i].a-1;j++)
                    {
                        if(board[j][y]!='\0')
                        {
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0)
                    {
                        winflag=-1;
                    }
                }
                break;
            }
        }
        if(x==1)
        {
            winflag++;
        }
        else if(x>1)//向上
        {
            int n1=n;
            char origin='\0';
            board[x][y]='\0';
            if(board[x-1][y]!='\0')//是存在棋子的
            {
                for(int i=1;i<=n1;i++)//刪除那個棋子的信息
                    {
                        if(c[i].a==x-1&&c[i].b==y)//僅僅是交換該i和最后一個,然后將n的值減小就ok,便於回復
                        {
                        origin=c[i].name;
                           exc=c[i];
                           c[i]=c[n];
                           c[n]=exc;
                            n1--;
                            break;
                        }
                    }
            }
            board[x-1][y]='Q';//將移到了新的位置
            c[0].a=x-1;
            if(Kill(c[0].a,c[0].b,n1))
            {
                winflag++;
            }
            board[x][y]='Q';//如果沒有刪除,那么不就是多添加了?
            board[x-1][y]=origin;
            c[0].a=x;
        }
        if(x==3)
        {
            winflag++;
        }
        else if(x<3)
        {
            int n1=n;
            char origin='\0';
            board[x][y]='\0';
            if(board[x+1][y]!='\0')//是存在棋子的
            {
                for(int i=1;i<=n1;i++)//刪除那個棋子的信息
                    {
                        if(c[i].a==x+1&&c[i].b==y)
                        {
                            origin=c[i].name;
                             exc=c[i];
                           c[i]=c[n];
                           c[n]=exc;
                            n1--;
                            break;
                        }
                    }
            }
            board[x+1][y]='Q';//將移到了新的位置
            c[0].a=x+1;
            if(Kill(c[0].a,c[0].b,n1))
            {
                winflag++;
            }
            board[x][y]='Q';
            board[x+1][y]=origin;
            c[0].a=x;
        }
        if(y==4)
        {
            winflag++;
        }
        else if(y>4)
        {
            int n1=n;
            char origin='\0';
            board[x][y]='\0';
            if(board[x][y-1]!='\0')
            {
                for(int i=1;i<=n1;i++)//刪除那個棋子的信息
                    {
                        if(c[i].a==x&&c[i].b==y-1)
                        {
                            origin=c[i].name;
                            exc=c[i];
                           c[i]=c[n];
                           c[n]=exc;
                            n1--;
                            break;
                        }
                    }
            }
            board[x][y-1]='Q';//將移到了新的位置
            c[0].b=y-1;
            if(Kill(c[0].a,c[0].b,n1))
            {
                winflag++;
            }
            board[x][y]='Q';
            board[x][y-1]=origin;
            c[0].b=y;

        }
        if(y==6)
        {
            winflag++;
        }
        else if(y<6)
        {
            int n1=n;
            char origin='\0';
            board[x][y]='\0';
            if(board[x][y+1]!='\0')
            {
                for(int i=1;i<=n1;i++)//刪除那個棋子的信息
                    {
                        if(c[i].a==x&&c[i].b==y+1)
                        {
                            origin=c[i].name;
                            exc=c[i];
                           c[i]=c[n];
                           c[n]=exc;
                            n1--;
                            break;
                        }
                    }
            }
            board[x][y+1]='Q';//將移到了新的位置
            c[0].b=y+1;
            if(Kill(c[0].a,c[0].b,n1))
            {
                winflag++;
            }
            board[x][y]='Q';
            board[x][y+1]=origin;
            c[0].b=y;
        }
        if(winflag==4)
        {
            printf("YES\n");
        }
        else
            printf("NO\n");
        scanf("%c",&cx);
    }
    return 0;
}

1 个解决方案

#1


因為提交了17次還沒過,我想問問大家到底我錯在哪里

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com