Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

ft,到底是哪里错呢?老是wa...

Posted by Ethanh at 2005-03-21 20:54:18 on Problem 2308
#include <iostream>
using namespace std;


bool Sright(char **draw,int n,int m,int a,int b,int x,int y,short depth);
bool Sleft(char **draw,int n,int m,int a,int b,int x,int y,short depth);
bool Sdown(char **draw,int n,int m,int a,int b,int x,int y,short depth);
bool Sup(char **draw,int n,int m,int a,int b,int x,int y,short depth);

bool Sleft(char **draw,int n,int m,int a,int b,int x,int y,short depth)//search left
{
	int c=b-1;
	
	if(!depth)
		return false;
	while(c>=0&&draw[a][c]=='*'){
		if(Sdown(draw,n,m,a,c,x,y,depth-1)||Sup(draw,n,m,a,c,x,y,depth-1))
			return true;
		c--;
	}
	if(c>=0&&draw[a][c]==draw[x][y]){
		draw[a][c]='*';
		draw[x][y]='*';
		return true;
	}
	return false;
}

bool Sright(char **draw,int n,int m,int a,int b,int x,int y,short depth)//search right
{
	int c=b+1;

	if(!depth)
		return false;
	while(c<m&&draw[a][c]=='*'){
		if(Sdown(draw,n,m,a,c,x,y,depth-1)||Sup(draw,n,m,a,c,x,y,depth-1))
			return true;
		c++;
	}
	if(c<m&&draw[a][c]==draw[x][y]){
		draw[a][c]='*';
		draw[x][y]='*';
		return true;
	}
	return false;
}

bool Sdown(char **draw,int n,int m,int a,int b,int x,int y,short depth)//search down
{
	int c=a+1;

	if(!depth)
		return false;
	while(c<n&&draw[c][b]=='*'){
		if(Sleft(draw,n,m,c,b,x,y,depth-1)||Sright(draw,n,m,c,b,x,y,depth-1))
			return true;
		c++;
	}
	if(c<n&&draw[c][b]==draw[x][y]){
		draw[c][b]='*';
		draw[x][y]='*';
		return true;
	}
	return false;
}

bool Sup(char **draw,int n,int m,int a,int b,int x,int y,short depth)//search up
{
	int c=a-1;

	if(!depth)
		return false;
	while(c>=0&&draw[c][b]=='*'){
		if(Sleft(draw,n,m,c,b,x,y,depth-1)||Sright(draw,n,m,c,b,x,y,depth-1))
			return true;
		c--;
	}
	if(c>=0&&draw[c][b]==draw[x][y]){
		draw[c][b]='*';
		draw[x][y]='*';
		return true;
	}
	return false;
}
int main()
{
	int n,m;
	char **draw;
	char flag,finish;

	cin>>n>>m;
	
	while(n||n){
	//initialize space
		draw=new char*[n]; 
		for(int i=0;i<n;i++)
			draw[i]=new char[m];

		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++)
				cin>>draw[j][k];
		}
        flag=1,finish=0;
		while(flag&&!finish){
			flag=0;//no pairs can be taken away
			finish=1;//no graph left
			for(int a=0;a<n;a++){
				for(int b=0;b<m;b++){
					if(draw[a][b]=='*')
						continue;
					finish=0;
					if(Sright(draw,n,m,a,b,a,b,3)||Sdown(draw,n,m,a,b,a,b,3)||
						Sleft(draw,n,m,a,b,a,b,3)||Sup(draw,n,m,a,b,a,b,3)){
						flag=1;
						break;
					}
					if(a==0||a==n-1){
						for(int k=b+1;k<m;k++){//right direction
							if(draw[a][b]==draw[0][k]){
								draw[a][b]='*';
								draw[0][k]='*';
								flag=1;
								break;
							}
							if(draw[a][b]==draw[n-1][k]){
								draw[a][b]='*';
								draw[n-1][k]='*';
								flag=1;
								break;
							}
						}
						for(int l=0;l<n;l++){//down direction
							if(draw[a][b]==draw[l][0]){
								draw[a][b]='*';
								draw[l][0]='*';
								flag=1;
								break;
							}
							if(draw[a][b]==draw[l][m-1]){
								draw[a][b]='*';
								draw[l][m-1]='*';
								flag=1;
								break;
							}
						}
					}
					if(b==0||b==m-1){
						for(int s=a+1;s<n;s++){//down direction
							if(draw[a][b]==draw[s][0]){
								draw[a][b]='*';
								draw[s][0]='*';
								flag=1;
								break;
							}
							if(draw[a][b]==draw[s][m-1]){
								draw[a][b]='*';
								draw[s][m-1]='*';
								flag=1;
								break;
							}
						}
						for(int t=0;t<m;t++){//right direction
							if(draw[a][b]==draw[0][t]){
								draw[a][b]='*';
								draw[0][t]='*';
								flag=1;
								break;
							}
							if(draw[a][b]==draw[n-1][t]){
								draw[a][b]='*';
								draw[n-1][t]='*';
								flag=1;
								break;
							}
						}
					}
				}
			}

		}
		if(finish)
			cout<<"yes"<<endl;
		else cout<<"no"<<endl;
		/*
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++)
				cout<<draw[i][j];
			cout<<"\n";
		}
		*/
		//delete space
		for(int p=0;p<n;p++)
			delete[] draw[p];
		delete[] draw;
		
		cin>>n>>m;
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator