本文主要是介绍L - Tic-Tac-Toe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Kim likes to play Tic-Tac-Toe.
Given a current state, and now Kim is going to take his next move. Please tell Kim if he can win the game in next 2 moves if both player are clever enough.
Here “next 2 moves” means Kim’s 2 move. (Kim move,opponent move, Kim move, stop).
Game rules:
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Each test case contains three lines, each line three string(“o” or “x” or “.”)(All lower case letters.)
x means here is a x
o means here is a o
. means here is a blank place.
Next line a string (“o” or “x”) means Kim is (“o” or “x”) and he is going to take his next move.
For each test case:
If Kim can win in 2 steps, output “Kim win!”
Otherwise output “Cannot win!”
3 . . . . . . . . . o o x o o . x x x o x o x . . o . . . x oSample Output
Cannot win! Kim win! Kim win!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ma[4][4];
char ch;
int panduan( )
{int a1,a2,a3,a4,a5,a6,a7,a8;a1=a2=a3=a4=a5=a6=a6=a8=0;if(ma[1][1]==ch){a1++,a4++;a8++;}if(ma[1][1]!=ch&&ma[1][1]!='.'){a1+=100,a4+=100,a8+=100;}if(ma[1][2]==ch){a1++,a5++;}if(ma[1][2]!=ch&&ma[1][2]!='.'){a1+=100,a5+=100;}if(ma[1][3]==ch){a1++,a6++,a7++;}if(ma[1][3]!=ch&&ma[1][3]!='.'){a1+=100,a6+=100,a7+=100;}if(ma[2][1]==ch){a2++,a4++;}if(ma[2][1]!=ch&&ma[2][1]!='.'){a2+=100,a4+=100;}if(ma[2][2]==ch){a2++,a5++,a7++,a8++;}if(ma[2][2]!=ch&&ma[2][2]!='.'){a2+=100,a5+=100,a7+=100,a8+=100;}if(ma[2][3]==ch){a2++,a6++;}if(ma[2][3]!=ch&&ma[2][3]!='.'){a2+=100,a6+=100;}if(ma[3][1]==ch){a3++,a4++,a7++;}if(ma[3][1]!=ch&&ma[3][1]!='.'){a3+=100,a4+=100,a7+=100;}if(ma[3][2]==ch){a3++,a5++;}if(ma[3][2]!=ch&&ma[3][2]!='.'){a3+=100,a5+=100;}if(ma[3][3]==ch){a3++,a6++,a8++;}if(ma[3][3]!=ch&&ma[3][3]!='.'){a3+=100,a6+=100,a8+=100;}if(a1>=1&&a1<=5){if(a4>=1&&a4<=6){if(ma[1][1]==ch&&a1==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[1][2]==ch&&a1==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[1][3]==ch&&a1==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[1][3]==ch&&a1==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[1][1]==ch&&a1==1&&a8==1)return 0;elsereturn 1;}}if(a2>=1&&a2<=5){if(a4>=1&&a4<=6){if(ma[2][1]==ch&&a2==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[2][2]==ch&&a2==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[2][3]==ch&&a2==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[2][2]==ch&&a2==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a1==1&&a8==1)return 0;elsereturn 1;}}if(a3>=1&&a3<=5){if(a4>=1&&a4<=6){if(ma[3][1]==ch&&a3==1&&a4==1)return 0;elsereturn 1;}if(a5>=1&&a5<=6){if(ma[3][2]==ch&&a3==1&&a5==1)return 0;elsereturn 1;}if(a6>=1&&a6<=6){if(ma[3][3]==ch&&a3==1&&a6==1)return 0;elsereturn 1;}if(a7>=1&&a7<=6){if(ma[3][1]==ch&&a3==1&&a7==1)return 0;elsereturn 1;;}if(a8>=1&&a8<=6){if(ma[3][3]==ch&&a3==1&&a8==1)return 0;elsereturn 1;}}if(a4>=1&&a4<=5){if(a7>=1&&a7<=6){if(ma[3][1]==ch&&a4==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[1][1]==ch&&a4==1&&a8==1)return 0;elsereturn 1;}}if(a5>=1&&a5<=5){if(a7>=1&&a7<=6){if(ma[2][2]==ch&&a5==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a5==1&&a8==1)return 0;elsereturn 1;}}if(a6>=1&&a6<=5){if(a7>=1&&a7<=6){if(ma[1][3]==ch&&a6==1&&a7==1)return 0;elsereturn 1;}if(a8>=1&&a8<=6){if(ma[3][3]==ch&&a6==1&&a8==1)return 0;elsereturn 1;}}if(a7>=1&&a7<=5){if(a8>=1&&a8<=6){if(ma[2][2]==ch&&a7==1&&a8==1)return 0;elsereturn 1;}}return 0;
}
int main()
{int n,m,t,i,j,flag;scanf("%d",&t);getchar();while(t--){for(i=1;i<=3;i++){for(j=1;j<=3;j++){scanf("%c",&ma[i][j]);if(ma[i][j]==' '||ma[i][j]=='\n')scanf("%c",&ma[i][j]);}}getchar();scanf("%c",&ch);if(ma[1][1]=='.'&&ma[1][2]==ch&&ma[1][3]==ch){printf("Kim win!\n");continue;}if(ma[2][1]=='.'&&ma[2][2]==ch&&ma[2][3]==ch){printf("Kim win!\n");continue;}if(ma[3][1]=='.'&&ma[3][2]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]=='.'&&ma[2][1]==ch&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][2]=='.'&&ma[2][2]==ch&&ma[3][2]==ch){printf("Kim win!\n");continue;}if(ma[1][3]=='.'&&ma[2][3]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]=='.'&&ma[2][2]==ch&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][3]=='.'&&ma[1][2]==ch&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[1][2]=='.'&&ma[1][3]==ch){printf("Kim win!\n");continue;}if(ma[2][1]==ch&&ma[2][2]=='.'&&ma[2][3]==ch){printf("Kim win!\n");continue;}if(ma[3][1]==ch&&ma[3][2]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][1]=='.'&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][2]==ch&&ma[2][2]=='.'&&ma[3][2]==ch){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[2][3]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][2]=='.'&&ma[3][3]==ch){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[1][2]=='.'&&ma[3][1]==ch){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[1][2]==ch&&ma[1][3]=='.'){printf("Kim win!\n");continue;}if(ma[2][1]==ch&&ma[2][2]==ch&&ma[2][3]=='.'){printf("Kim win!\n");continue;}if(ma[3][1]==ch&&ma[3][2]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][1]==ch&&ma[3][1]=='.'){printf("Kim win!\n");continue;}if(ma[1][2]==ch&&ma[2][2]==ch&&ma[3][2]=='.'){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[2][3]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][1]==ch&&ma[2][2]==ch&&ma[3][3]=='.'){printf("Kim win!\n");continue;}if(ma[1][3]==ch&&ma[1][2]==ch&&ma[3][1]=='.'){printf("Kim win!\n");continue;}flag=0;flag = panduan();if(flag)printf("Kim win!\n");if(!flag)printf("Cannot win!\n");}return 0;
}
这篇关于L - Tic-Tac-Toe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!