ACM-ICPC 4836 Gomoku
2014-04-30 17:09
183 查看
暴力枚举了所有情况......不知道有没有更好的做法
/*author: birdstorm*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#define MAXN 1005
#define N 205
#define INF 1<<30
#define eps 1.0e-10
#define For(i,m,n) for(i=(m);i<n;i++)
#define MAX(x,y) (x)>(y)?(x):(y)
#define MIN(x,y) (x)<(y)?(x):(y)
int a[20][20], goalx, goaly;
int over()
{
int i, j, temp;
For(i,0,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i][j+1]==temp&&a[i][j+2]==temp&&a[i][j+3]==temp&&a[i][j+4]==temp) return 1;
}
For(j,0,15) For(i,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j]==temp&&a[i+2][j]==temp&&a[i+3][j]==temp&&a[i+4][j]==temp) return 1;
}
For(i,0,11) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j+1]==temp&&a[i+2][j+2]==temp&&a[i+3][j+3]==temp&&a[i+4][j+4]==temp) return 1;
}
For(i,4,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i-1][j+1]==temp&&a[i-2][j+2]==temp&&a[i-3][j+3]==temp&&a[i-4][j+4]==temp) return 1;
}
return 0;
}
int judge(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(over()){
goalx=i; goaly=j;
ans++;
}
a[i][j]=0;
}
return ans;
}
int judgetie(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(judge(player)>=2&&judge(-player)==0){
goalx=i;
goaly=j;
return 1;
}
a[i][j]=0;
}
return 0;
}
main()
{
int i, j, k, n, p, q, x, y;
int black, white, count, next;
while(scanf("%d",&n),n){
For(i,0,15) For(j,0,15) a[i][j]=0;
black=white=count=0;
For(i,0,n){
scanf("%d%d%d",&x,&y,&p);
q=p==0?-1:1;
a[x][y]=q;
if(q==1) black++;
else white++;
}
if(black<white||black>white+1){
puts("Invalid.");
continue;
}
next=black==white?1:-1;
if(judge(next)>=1) printf("Place %s at (%d,%d) to win in 1 move.\n",next==1?"black":"white",goalx,goaly);
else if(judge(-next)>=2) puts("Lose in 2 moves.");
else if(judgetie(next)) printf("Place %s at (%d,%d) to win in 3 moves.\n",next==1?"black":"white",goalx,goaly);
else puts("Cannot win in 3 moves.");
}
return 0;
}
/*author: birdstorm*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#define MAXN 1005
#define N 205
#define INF 1<<30
#define eps 1.0e-10
#define For(i,m,n) for(i=(m);i<n;i++)
#define MAX(x,y) (x)>(y)?(x):(y)
#define MIN(x,y) (x)<(y)?(x):(y)
int a[20][20], goalx, goaly;
int over()
{
int i, j, temp;
For(i,0,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i][j+1]==temp&&a[i][j+2]==temp&&a[i][j+3]==temp&&a[i][j+4]==temp) return 1;
}
For(j,0,15) For(i,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j]==temp&&a[i+2][j]==temp&&a[i+3][j]==temp&&a[i+4][j]==temp) return 1;
}
For(i,0,11) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i+1][j+1]==temp&&a[i+2][j+2]==temp&&a[i+3][j+3]==temp&&a[i+4][j+4]==temp) return 1;
}
For(i,4,15) For(j,0,11) if(a[i][j]){
temp=a[i][j];
if(a[i-1][j+1]==temp&&a[i-2][j+2]==temp&&a[i-3][j+3]==temp&&a[i-4][j+4]==temp) return 1;
}
return 0;
}
int judge(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(over()){
goalx=i; goaly=j;
ans++;
}
a[i][j]=0;
}
return ans;
}
int judgetie(int player)
{
int i, j, ans=0;
For(i,0,15) For(j,0,15) if(a[i][j]==0) {
a[i][j]=player;
if(judge(player)>=2&&judge(-player)==0){
goalx=i;
goaly=j;
return 1;
}
a[i][j]=0;
}
return 0;
}
main()
{
int i, j, k, n, p, q, x, y;
int black, white, count, next;
while(scanf("%d",&n),n){
For(i,0,15) For(j,0,15) a[i][j]=0;
black=white=count=0;
For(i,0,n){
scanf("%d%d%d",&x,&y,&p);
q=p==0?-1:1;
a[x][y]=q;
if(q==1) black++;
else white++;
}
if(black<white||black>white+1){
puts("Invalid.");
continue;
}
next=black==white?1:-1;
if(judge(next)>=1) printf("Place %s at (%d,%d) to win in 1 move.\n",next==1?"black":"white",goalx,goaly);
else if(judge(-next)>=2) puts("Lose in 2 moves.");
else if(judgetie(next)) printf("Place %s at (%d,%d) to win in 3 moves.\n",next==1?"black":"white",goalx,goaly);
else puts("Cannot win in 3 moves.");
}
return 0;
}
相关文章推荐
- hdu 5876 Sparse Graph(补图最短路) 2016 ACM/ICPC Asia Regional Dalian Online 1009
- 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ
- HDU 5573 Binary Tree ACM/ICPC 2015 Shanghai(构造)
- HDU 5876 Sparse Graph(2016 ACM/ICPC Asia Regional Dalian Online)
- 2011 ACM/ICPC 北京赛区现场赛解题:Peach Blossom Spring
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
- 2016 ACM/ICPC Dalian Online-1002 Different GCD Subarray Query
- 2015 ACM-ICPC 上海大都会赛简要总结
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 G. Xor
- 2016 ACM/ICPC Asia Regional Qingdao Online 1002Cure
- DP基础问题若干(ACM/ICPC) 来自http://blog.csdn.net/accry/article/details/6607593
- HDU 5883 The Best Path 2016 ACM/ICPC Asia Regional Qingdao Online 1006d
- 【2016 ACM/ICPC Asia Regional Qingdao Online】
- 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online
- 【题解】 2016 ACM/ICPC Asia Regional Qingdao Online (6+5)
- 2016 ACM/ICPC Asia Regional Shenyang Online && hdoj5901 Count primes Lehmer
- 近几年ACM/ICPC区域赛铜牌题
- G - Grade (sort) 2014 ACM/ICPC Asia Regional Beijing Online
- 【2016 ACM/ICPC Asia Regional Qingdao Online】