赞
踩
- #include<iomanip>
- #include<iostream>
- #include<cmath>
- #include<string>
- #include<cstring>
- #include<algorithm>
- #include<cstdio>
- #include<vector>
- #include<stack>
- #include<map>
- #include<queue>
- #define ll long long
- using namespace std;
- ll n,a[1000][1000],x,y,b[1000][1000];
- void init(){
- for(int i=0;i<1000;i++) for(int j=0;j<1000;j++) a[i][j]=2;
- }
- bool find(){
- //竖着
- ll sum=1,i,j;
- i=x,j=y;
- b[i][j]=3;
- while(1){
- if(a[i+1][j]==1){
- i++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i-1][j]==1){
- i--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //横着
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i][j+1]==1){
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i][j-1]==1){
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //斜着 1
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i-1][j-1]==1){
- i--;
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i+1][j+1]==1){
- i++;
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //斜着 2
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i-1][j+1]==1){
- i--;
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i+1][j-1]==1){
- i++;
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- return false;
- }
- bool find2(){
- //竖着
- ll sum=1,i,j;
- i=x,j=y;
- b[i][j]=3;
- while(1){
- if(a[i+1][j]==0){
- i++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i-1][j]==0){
- i--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //横着
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i][j+1]==0){
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i][j-1]==0){
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //斜着 1
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i-1][j-1]==0){
- i--;
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i+1][j+1]==0){
- i++;
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- //斜着 2
- sum=1;
- i=x,j=y;
- while(1){
- if(a[i-1][j+1]==0){
- i--;
- j++;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- i=x,j=y;
- while(1){
- if(a[i+1][j-1]==0){
- i++;
- j--;
- sum++;
- b[i][j]=3;
- }
- else break;
- }
- if(sum>=5) return true;
- return false;
- }
- bool find3(){
- for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]==2) return false;
- return true;
- }
- int main(){
- init();
- cout<<"棋盘大小:";
- cin>>n;
- cout<<"白子为0,黑子为1,2为空棋位\n下棋时请输入棋的坐标(格式:x y 坐标范围是从1,1到n,n)\n如果一方赢了那他赢得那一行棋都会变成3\n";
- while(1){
- cout<<" ";
- for(int i=1;i<=n;i++) printf("%3d",i);
- cout<<"\n\n";
- for(int i=1;i<=n;i++){
- printf("%3d",i);
- for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
- cout<<"\n";
- }
- if(find3()){
- cout<<"平局,游戏结束";
- break;
- }
- //黑子
- cout<<"该黑子了\n";
- cin>>x>>y;
- while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
- if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
- else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
- cout<<"该黑子了\n";
- cin>>x>>y;
- }
- a[x][y]=1;
- for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
- if(find()){
- cout<<" ";
- for(int i=1;i<=n;i++) printf("%3d",i);
- cout<<"\n\n";
- for(int i=1;i<=n;i++){
- printf("%3d",i);
- for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
- cout<<"\n";
- }
- cout<<"黑子赢了,游戏结束";
- break;
- }
- cout<<" ";
- for(int i=1;i<=n;i++) printf("%3d",i);
- cout<<"\n\n";
- for(int i=1;i<=n;i++){
- printf("%3d",i);
- for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
- cout<<"\n";
- }
- if(find3()){
- cout<<"平局,游戏结束";
- break;
- }
- //白子
- cout<<"该白子了\n";
- cin>>x>>y;
- while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
- if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
- else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
- cout<<"该白子了\n";
- cin>>x>>y;
- }
- a[x][y]=0;
- for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
- if(find2()){
- cout<<" ";
- for(int i=1;i<=n;i++) printf("%3d",i);
- cout<<"\n\n";
- for(int i=1;i<=n;i++){
- printf("%3d",i);
- for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
- cout<<"\n";
- }
- cout<<"白子赢了,游戏结束";
- break;
- }
- }
- cout<<"\n ";
- for(int i=1;i<=n;i++) printf("%3d",i);
- cout<<"\n\n";
- for(int i=1;i<=n;i++){
- printf("%3d",i);
- for(int j=1;j<=n;j++) printf("%3d",b[i][j]);
- cout<<"\n";
- }
- return 0;
- }
用数字代表棋子
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。