寒假集訓 Day2 D CodeForces - 825B:五子棋

1.21 D題 CodeForces - 825B

題目大意:

給一個10*10棋盤,上面有棋子 X 和 0, 找出在一個空處放一個子使X連成五子或以上。

一個輸入

解題思路:

正向思維:

1.找四個連一起的,然後判斷是否可以在兩端空位加棋子,這就要判斷兩端是否有空位;
2.找在中間加一個棋子可以把兩個連在一起到比5大的。

反向思維:

找空位,判斷垂直、水平、斜向是否有四個子或以上。
這個思路實現起來很容易,不過我在實現的時候重用了很多代碼,寫得很難看。

AC代碼

#include<cstdio>
#include<iostream>
#include<queue>
#include<string>
#include<cmath>
#include<algorithm>
#include<limits.h>
#define rep(i,l,p) for(int i=l; i<=p; i++)
#define drep(i,p,l) for(int i=p; i>=l; i--)
using namespace std;

struct node{
    int x,y;
    node(int i,int j){
        x = i;
        y = j;
    }
};
int n;
int a[13][13];
queue<node> que;
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    char t;
    rep(i,1,10){
        rep(j,1,10){
            cin >> t;
            if ( t == 'X') a[i][j] = 1;
            else if (t == 'O') a[i][j] = -1;    
            else {
                node *tt = new node(i,j);
                que.push(*tt);
            }
        } 
    }   
//  rep(i,1,10){
//      rep(j,1,10) printf("%d ",a[i][j]);
//      printf("\n");
//  }
    bool flag = false;
    while( !que.empty() ){

        node tmp = que.front();
        que.pop();

        int x = tmp.x, y = tmp.y; 
            //  printf("x:%d  y:%d\n",x,y);
        int res = 0;
        while( !flag ){
            y--;
            if (y < 1 ) break; 
        //  printf("a[%d][%d]:%d\n",x,y,a[x][y]);
            if ( a[x][y] == 1 ) res++;
            else break;
            if (res>=4) {
                flag = true;
                break;          
            }
        } 
        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        while( !flag ){
            y++;
            if (y>10) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }
        }
        //printf("%d\n",res);
        if ( flag ) break;
        x = tmp.x; y = tmp.y; 
        res = 0;
        while( !flag ){
            x--;
            if (x < 1 ) break; 
            if ( a[x][y] == 1 ) res++;
            else break;
            if (res>=4) {
                flag = true;
                break;          
            }
        } 
        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        while( !flag ){
            x++;
            if (x>10) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }
        }

        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        res = 0;
        while( !flag ){
            x--;
            y--;
            if ( x <1 || y <1) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }           
        }
        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        while( !flag ){
            x++;
            y++;
            if ( x >10 || y >10) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }           
        }



        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        res = 0;
        while( !flag ){
            x--;
            y++;
            if ( x <1 || y >10) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }           
        }
        if ( flag ) break;
        x = tmp.x; y = tmp.y;
        while( !flag ){
            x++;
            y--;
            if ( x >10 || y <1) break;
            if (a[x][y] == 1) res++;
            else break;
            if (res>=4){
                flag = true;
                break;
            }           
        }
    }

    if( flag ) printf("YES\n");
    else printf("NO\n"); 
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章