UVa 220 黑白棋 算法竞赛入门经典 习题4-3

输出格式输出格式输出格式!UVaOJ的输出控制让我想起了玩黑魂时的感觉。
这道题难点在M操作后的输出 “Black - ” 一眼看去‘-’后面是两个个空格,然而事实是-与后面的数字一共占3列。你自己调试的时候,发现怎样都对,而提交总会无限Presentation error。当你花半天udebug的时候,对比结果会告诉你“我这里不是两个空格!哈哈!没想到吧!“[白眼][白眼]。

原题地址
样例调试udebug

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string cc[12] ={""};
int aa[12][12];
int a[8] = {1,1,1,0,0,-1,-1,-1};
int b[8] = {-1,0,1,1,-1,0,1,-1};
int lg(int u);
int mo(char c1,char c2,int u);
int main()
{
	int n = 0;
	cin>>n;
	int pp = 0;
	for(int nn = 0;nn<n;++nn)
	{
		if(pp++)
		  {
		  	cout<<endl;
		  }
		for(int i = 1;i<=8;++i)
		   cin>>cc[i];
		int u = 1;
		char c;
		cin>>c;
		if(c == 'B') u = -1;
		string s;
		while(cin>>s && s[0]!='Q')
		{
			if(s[0] == 'L')
			  {
			  	int f = lg(u);
			  	if(!f) {
			  		u=u*(-1);
			  		cout<<"No legal move."<<endl;
				  }
				else{
					int de = 0;
				   for(int i = 1;i<=8;++i)
	                for(int j = 0;j<8;++j)
	                    if(aa[i][j] == 1)
	                  {    
                          if(de++) {
                          	cout<<' ';
						  }
	     		             cout<<'('<<i<<','<<j+1<<')';
		              }
	    
	  	             cout<<endl;
				}
			  	for(int lp = 0;lp<12;++lp) 
			  	   memset(aa[lp],0,sizeof(aa[lp]));
			  }
			if(s[0] == 'M')
			   {
			   	if(mo(s[1],s[2],u))
				   {
				   		int wc = 0;
	                     int bc = 0;
	                for(int i = 1;i<=8;++i)
	                   {
	  	                  for(int j = 0;j<8;++j)
	                      {
	    	                 if(cc[i][j] == 'W')
	    	                    ++wc;
	    	                 if(cc[i][j] == 'B')
	    	                     ++bc;
		                    }
	                     }		   	          
				   	 cout<<"Black - ";
				   	 if(bc/10 < 1) 
				   	   cout<<' ';	
					 cout<<bc<<' ';
					 cout<<"White - ";
				   	 if(wc/10 < 1) 
				   	   cout<<' ';
					 cout<<wc<<endl;
				   };
			   	u = u*(-1);
			   }
		}
		for(int i = 1;i<=8;++i)
		  	cout<<cc[i]<<endl;
	}
}
int lg(int u)
{
	char e;
	char v;
	e = u==1?'W':'B';
	v = u==1?'B':'W';
	for(int i = 1;i<=8;++i)
	{
	  for(int j = 0;j<8;++j)
	  {
	     if(cc[i][j] == e)
	       {
		     for(int x = 0;x<8;++x)
		     {
		     	if(i+a[x]>=1 && i+a[x]<=8 && j+b[x]>=0 && j+b[x] <=7)
		      {
		     	if(cc[i+a[x]][j+b[x]] == v)
	          {
	          	int ax = i+a[x];
	            int ay = j+b[x];
	          	while((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == v)
                 {
                 	ax+=a[x];
                 	ay+=b[x];
				 }
				if((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == '-')
				  aa[ax][ay] = 1;
			  }
		    }
			 }

		   }
		}
	 }
	 int fag = 0;
	 for(int i = 1;i<=8;++i)
	   for(int j = 0;j<8;++j)
	     if(aa[i][j] == 1)
	     	 fag = 1;
	  if(fag == 0)
	  	return 0;
	  return 1;
	
}
int mo(char c1,char c2,int u)
{

	int ae = c1-'0';
	int as = c2-'0';
	char e;
	char v;
	e = u==1?'W':'B';
	v = u==1?'B':'W';
	cc[ae][as-1] = e;
	for(int x = 0;x<8;++x)
	{
		int cn = 0;
	   if(ae+a[x]>=1 && ae+a[x]<=8 && as-1+b[x]>=0 && as-1+b[x] <=7)
		{
		  if(cc[ae+a[x]][as-1+b[x]] == v)
	      {
	        int ax = ae+a[x];
	        int ay = as-1+b[x];
	        ++cn;
	        while((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == v)
            {
              ax+=a[x];
              ay+=b[x];
              ++cn;
			}
			if((ax>=1 && ax<=8 && ay>=0 && ay<=7) && cc[ax][ay] == e)
			{
				for(int i =1;i<cn;++i)
				  cc[ae+i*a[x]][as-1+i*b[x]] = e;
			}  
		  }
		  }
	}
   return 1; 	  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值