Problem 1249 : Make a Sequence

Problem B: Make a Sequence
http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=1249

  • 問題概要

3D五目並べ的なもの。大きさと並べる長さは変動する。
勝者とその勝ちが決定するターンを求めよ。

  • 解法

かくだけ。
判定するときの13方向というのがちょっとあれかも。

    int dx[] = {0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1, -1};
    int dy[] = {0,  1,  1,  1,  0,  0,  0,  1, -1,  1,  1, -1,  1};
    int dz[] = {1,  0,  1, -1,  0,  1, -1,  0,  0,  1, -1,  1,  1};

これですべての方向表せる。

  • ソース
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<sstream>
#include<cassert>
#include<queue>
#include<stack>

#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(C)     (C).begin(),(C).end()
#define pb         push_back
#define mp         make_pair
#define vint       vector<int>
#define FOR(it,o)  for(__typeof((o).begin()) it=(o).begin(); it!=(o).end(); ++it)
#define lli         long long int
#define ld          long double

using namespace std;

class Puzzle{
public:
  int t[7][7][7], n, m;
  Puzzle(int size, int length){
    n = size;
    m = length;
    rep(i, n){
      rep(j, n){
	rep(k, n){
	  t[i][j][k] = -1;
	}
      }
    }
  }
  bool correct(int x, int y, int z){
    if(x < 0 || y < 0 || z < 0){
      return false;
    }
    if(x >= n || y >= n || z >= n){
      return false;
    }
    return true;
  }
  int win(){  
    int dx[] = {0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1, -1};
    int dy[] = {0,  1,  1,  1,  0,  0,  0,  1, -1,  1,  1, -1,  1};
    int dz[] = {1,  0,  1, -1,  0,  1, -1,  0,  0,  1, -1,  1,  1};
    int ans = -1;
    bool end = false;
    rep(i, n){
      rep(j, n){
	rep(k, n){
	  int now = t[i][j][k];
	  if(now == -1)continue;
	  rep(d, 13){
	    int nx = i;
	    int ny = j;
	    int nz = k;
	    rep(l, m-1){
	      nx += dx[d];
	      ny += dy[d];
	      nz += dz[d];
	      if(!correct(nx, ny, nz) || t[nx][ny][nz] != now ){
		break;
	      }
	      if(l == m-2){
		end = true;
		ans = now;
	      }
	    }
	    if(end)break;
	  }
	  if(end)break;
	}
	if(end)break;
      }
      if(end)break;
    }
    return ans;
  }
  void add(int x, int y, int c){
    int cnt=0;
    while(t[x][y][cnt] != -1){
      cnt++;
    }
    t[x][y][cnt] = c;
  }
};
	  

main(){
  int n, m, turn;
  string s[] ={"Black ", "White "};
  while(cin >> n >> m >> turn){
    if( n+ m + turn == 0)break;
    Puzzle p(n, m);
    bool end =false;
    rep(i, turn){
      int x, y;
      cin >> x >> y;
      if(end)continue;
      p.add(x-1, y-1, i%2);
      int winner = p.win();
      if(winner != -1){
	cout << s[winner] << i+1 << endl;
	end = true;
      }
    }
    if(!end)cout << "Draw" << endl;
  }
}

if(end)break;のとこがもうすこしなんとかならないものか。