zoukankan      html  css  js  c++  java
  • 紫书第四章训练 UVA 220 Othello by 16zcw

    来源:http://blog.csdn.net/qq_26083215/article/details/71149557

    Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side and black on the other. One player places disks with the white side up and the other player places diskswith the black side up. The players alternate placing one disk on an unoccupied space on the board.In placing a disk, the player must bracket at least one of the other color disks. Disks are bracketedif they are in a straight line horizontally, vertically, or diagonally, with a disk of the current player’s color at each end of the line. When a move is made, all the disks that were bracketed are changed to the color of the player making the move. (It is possible that disks will be bracketed across more than one line in a single move.)


    Input

    The first line of the input is the number of games to be processed. Each game consists of a board configuration followed by a list of commands. The board configuration consists of 9 lines. The first 8 pecify the current state of the board. Each of these 8 lines contains 8 characters, and each of these characters will be one of the following:

    ‘-’ indicating an unoccupied square

    ‘B’ indicating a square occupied by a black disk

    ‘W’ indicating a square occupied by a white disk

    The ninth line is either a ‘B’ or a ‘W’ to indicate which is the current player. You may assume that the data is legally formatted. Then a set of commands follows. The commands are to list all possible moves for the current player,make a move, or quit the current game. There is one command per line with no blanks in the input

    Output

    The commands and the corresponding outputs are formatted as follows:
    List all possible moves for the current player. The command is an ‘L’ in the first column of the line. The program should go through the board and print all legal moves for the current player in the format (x, y) where x represents the row of the legal move and y represents its column. These moves should be printed in row major order which means:1) all legal moves in row number i will be printed before any legal move in row number j if j is greater than i and 2) if there is more than one legal move in row number i, the moves will be printed in ascending order based on column number.
    All legal moves should be put on one line. If there is no legal move because it is impossible for the current player to bracket any pieces, the program should print the message ‘No legal move.’ Make a move. The command is an ‘M’ in the first column of the line, followed by 2 digits in the second and third column of the line. The digits are the row and the column of the space to place the piece of the current player’s color, unless the current player has no legal move. If the current player has no legal move, the current player is first changed to the other player and the move will be the move of the new current player. You may assume that the move is then legal. You should record the changes to the board, including adding the new piece and changing the color of all bracketed pieces. At the end of the move, print the number of pieces of each color on the board in the format ‘Black - xx White - yy’ where xx is the number of black pieces on the board and yy is the number of white pieces on the board. After a more, the current player will be changed to the player that did not move.
    Quit the current game. The command will be a ‘Q’ in the first column of the line. At this point,print the final board configuration using the same format as was used in the input. This terminates input for the current game.
    You may assume that the commands will be syntactically correct. Put one blank line betweenoutput from separate games and no blank lines anywhere else in the output.


    Sample Input
    2
    --------
    --------
    --------
    ---WB---
    ---BW---
    --------
    --------
    --------
    W
    L
    M35
    L
    Q
    WWWWB---
    WWWB----
    WWB-----
    WB------
    --------
    --------
    --------
    --------
    B
    L
    M25
    L
    Q
    Sample Output
    (3,5) (4,6) (5,3) (6,4)
    Black - 1 White - 4
    (3,4) (3,6) (5,6)
    --------
    --------
    ----W---
    ---WW---
    ---BW---
    --------
    --------
    --------
    No legal move.
    Black - 3 White - 12
    (3,5)
    WWWWB---
    WWWWW---
    WWB-----
    WB------
    --------
    --------
    --------

    --------

    题意:

    模拟黑白棋走法,规则与黑白棋一致。

    第一行W和B表示下的人的棋子

    L输出可以走的点,没有可以走输出No legal move.

    Q是退出输出改变后的棋盘

    M之后输入下的人要下的子(如果之前L没有可以下的则下的人变更),如果下错,继续让下的人下

    分析:

    直接模拟,注意输出格式。

    方向是8个方向,最后才反应过来不是4个。

    如果wa一次的话,还是重打吧,调试会出人命的(除了PE一次过真幸运,话说那个竟然是 %2d,为什么复制原来输出时只有一个空格)。

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<iostream>
      4 using namespace std;
      5 char a[10][10];
      6 int d[64][2];
      7 int sum;
      8 
      9 char changeplayer(char c)//交换用户 
     10 {
     11     if(c=='B')return 'W';
     12     return 'B';
     13 }
     14 //搜索x,y点往各个方向是否存在可以改换棋子的 
     15 int upright(int x,int y,char c)//右上 
     16 {
     17     int i,d=0;
     18     for(i=1;x-i>=1&&y+i<=8;i++)
     19     {
     20         if(a[x-i][y+i]==c)
     21         {
     22             if(d)return i;
     23             else return 0;
     24         }
     25         else if(a[x-i][y+i]=='-')return 0;
     26         d++;
     27     }
     28     return 0;
     29 }
     30 int upleft(int x,int y,char c)//左上 
     31 {
     32     int i,d=0;
     33     for(i=1;x-i>=1&&y-i>=1;i++)
     34     {
     35         if(a[x-i][y-i]==c)
     36         {
     37             if(d)return i;
     38             else return 0;
     39         }
     40         else if(a[x-i][y-i]=='-')return 0;
     41         d++;
     42     }
     43     return 0;
     44 }
     45 int downleft(int x,int y,char c)//左下 
     46 {
     47     int i,d=0;
     48     for(i=1;x+i<=8&&y-i>=1;i++)
     49     {
     50         if(a[x+i][y-i]==c)
     51         {
     52             if(d)return i;
     53             else return 0;
     54         }
     55         else if(a[x+i][y-i]=='-')return 0;
     56         d++;
     57     }
     58     return 0;
     59 }
     60 int downright(int x,int y,char c)//右下 
     61 {
     62     int i,d=0;
     63     for(i=1;x+i<=8&&y+i<=8;i++)
     64     {
     65         if(a[x+i][y+i]==c)
     66         {
     67             if(d)return i;
     68             else return 0;
     69         }
     70         else if(a[x+i][y+i]=='-')return 0;
     71         d++;
     72     }
     73     return 0;
     74 }
     75 int up(int x,int y,char c)//
     76 {
     77     int i,d=0;
     78     for(i=x-1;i>=1;i--)
     79     {
     80         if(a[i][y]==c)
     81         {
     82             if(d)return i;
     83             else return 0;
     84         }
     85         else if(a[i][y]=='-')return 0;
     86         d++;
     87     }
     88     return 0;
     89 }
     90 int down(int x,int y,char c)//
     91 {
     92     int i,d=0;
     93     for(i=x+1;i<=8;i++)
     94     {
     95         if(a[i][y]==c)
     96         {
     97             if(d)return i;
     98             else return 0;
     99         }
    100         else if(a[i][y]=='-')return 0;
    101         d++;
    102     }
    103     return 0;
    104 }
    105 int left(int x,int y,char c)//
    106 {
    107     int i,d=0;
    108     for(i=y-1;i>=1;i--)
    109     {
    110         if(a[x][i]==c)
    111         {
    112             if(d)return i;
    113             else return 0;
    114         }
    115         else if(a[x][i]=='-')return 0;
    116         d++;
    117     }
    118     return 0;
    119 }
    120 int right(int x,int y,char c)//
    121 {
    122     int i,d=0;
    123     for(i=y+1;i<=8;i++)
    124     {
    125         if(a[x][i]==c)
    126         {
    127             if(d)return i;
    128             else return 0;
    129         }
    130         else if(a[x][i]=='-')return 0;
    131         d++;
    132     }
    133     return 0;
    134 }
    135 void change(int x,int y,char c)//改变棋子 
    136 {
    137     int up1,down1,left1,right1,upleft1,upright1,downleft1,downright1,i;
    138     up1=up(x,y,c);
    139     if(up1)
    140     {
    141         for(i=x;i>=up1;i--)
    142         {
    143             a[i][y]=c;
    144         }
    145     }
    146     down1=down(x,y,c);
    147     if(down1)
    148     {
    149         for(i=x;i<=down1;i++)
    150         {
    151             a[i][y]=c;
    152         }
    153     }
    154     left1=left(x,y,c);
    155     if(left1)
    156     {
    157         for(i=y;i>=left1;i--)
    158         {
    159             a[x][i]=c;
    160         }
    161     }
    162     right1=right(x,y,c);
    163     if(right1)
    164     {
    165         for(i=y;i<=right1;i++)
    166         {
    167             a[x][i]=c;
    168         }
    169     }
    170     upleft1=upleft(x,y,c);
    171     if(upleft1)
    172     {
    173         for(i=0;i<=upleft1;i++)
    174         {
    175             a[x-i][y-i]=c;
    176         }
    177     }
    178     upright1=upright(x,y,c);
    179     if(upright1)
    180     {
    181         for(i=0;i<=upright1;i++)
    182         {
    183             a[x-i][y+i]=c;
    184         }
    185     }
    186     downleft1=downleft(x,y,c);
    187     if(downleft1)
    188     {
    189         for(i=0;i<=downleft1;i++)
    190         {
    191             a[x+i][y-i]=c;
    192         }
    193     }
    194     downright1=downright(x,y,c);
    195     if(downright1)
    196     {
    197         for(i=0;i<=downright1;i++)
    198         {
    199             a[x+i][y+i]=c;
    200         }
    201     }
    202 }
    203 void out()//输出可以下的棋子的位置 
    204 {
    205     for(int i=0;i<sum;i++)
    206     {
    207         if(i)printf(" ");
    208         printf("(%d,%d)",d[i][0],d[i][1]);
    209     }
    210     puts("");
    211 }
    212 void out2()//输出每人的棋子个数 
    213 {
    214     int i,j,s1,s2;
    215     s1=s2=0;
    216     for(i=1;i<=8;i++)
    217     {
    218         for(j=1;j<=8;j++)
    219         {
    220             if(a[i][j]=='B')s1++;
    221             else if(a[i][j]=='W')s2++;
    222         }
    223     }
    224     printf("Black - %2d White - %2d
    ",s1,s2);
    225 }
    226 void find(char x)//寻找是否可下 
    227 {
    228     int i,j;
    229     for(i=1;i<=8;i++)
    230     {
    231         for(j=1;j<=8;j++)
    232         {
    233             if(a[i][j]=='-')
    234             {
    235                 if(left(i,j,x)||right(i,j,x)||up(i,j,x)||down(i,j,x)||upright(i,j,x)||upleft(i,j,x)||downright(i,j,x)||downleft(i,j,x))
    236                 {
    237                     d[sum][0]=i;
    238                     d[sum][1]=j;
    239                     sum++;
    240                 }
    241             }
    242         }
    243     }
    244 }
    245 int main()
    246 {
    247     char k[5];
    248     int t,i,j,ll=0;
    249     char player;
    250     scanf("%d",&t);
    251     while(t--)
    252     {
    253         if(ll)puts("");
    254         ll=1;
    255         for(i=1;i<=8;i++)
    256         {
    257             scanf("%s",a[i]+1);
    258         }
    259         scanf("%s",k);
    260         sum=0;
    261         player=k[0];
    262         find(player);//寻找可下点 
    263         while(scanf("%s",k))
    264         {
    265             if(k[0]=='Q')
    266             {
    267                 for(i=1;i<=8;i++)
    268                 {
    269                     for(j=1;j<=8;j++)
    270                     {
    271                         printf("%c",a[i][j]);
    272                     }
    273                     puts("");
    274                 }
    275                 break;
    276             }
    277             //sum==0,没有可以下的位置 
    278             if(k[0]=='L')
    279             {
    280                 if(sum==0)cout<<"No legal move."<<endl;
    281                 else out();
    282             }
    283             else
    284             {
    285                 if(sum==0)
    286                 {
    287                     player=changeplayer(player);
    288                     sum=0;
    289                     find(player);
    290                 }
    291                 int flag=0;//寻找是否下的位置可下 
    292                 for(i=0;i<sum;i++)
    293                 {
    294                     if(d[i][0]==k[1]-'0'&&d[i][1]==k[2]-'0')
    295                     {
    296                         flag=1;break;
    297                     }
    298                 }
    299                 if(flag)
    300                 {
    301                     change(k[1]-'0',k[2]-'0',player);
    302                     player=changeplayer(player);
    303                     out2();
    304                     sum=0;
    305                     find(player);
    306                 }
    307             }
    308         }
    309     }
    310 }
  • 相关阅读:
    python自动化测试学习路线-python设计语言sys模块argv参数用法
    python自动化测试学习路线-python设计语言serial模块调用方法
    【考研复习】线性代数矩阵部分-题解
    【考研复习
    Windows提权
    Hash算法——加密解密说明
    AES 加密算法的原理详解
    sqlmap常用命令
    curl的使用
    DOM XSS详解
  • 原文地址:https://www.cnblogs.com/tzcacm/p/6806350.html
Copyright © 2011-2022 走看看