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

总是过不了呀

Posted by 36211316 at 2007-03-01 14:20:41 on Problem 1185
#include<stdio.h>
#include<stdlib.h>

int m,n,step;
int place(int x,int y,char *str){
	int i;
	if(str[x*n+y]!='P')
		return 0;
	for(i=x;i>=0&&i>x-3;--i)
		if(str[i*n+y]=='T')
			return 0;
	for(i=y;i>=0&&i>y-3;--i)
		if(str[x*n+i]=='T')
			return 0;
	for(i=x;i<m&&i<x+3;++i)
		if(str[i*n+y]=='T')
			return 0;
	for(i=y;i<n&&i<y+3;++i)
		if(str[x*n+i]=='T')
			return 0;
	return 1;

}
int search(int x,int y,int t,char*str){
     int i,j;
     int result=0;
     
     if(t==step)
         return 1;
     else
        if(place(x,y,str)){
            str[x*n+y]='T';
            for(i=0;i<m;++i)
                for(j=0;j<n;++j){
                    result+=search(i,j,t+1,str);
                    if(result>0)
                        break;  
                }    
            if(result>0)
                return 1;
            else
            {
                str[x*n+y]='P';
                return 0;
            }
        }
      else
          return 0;
}

int main(){
     int i=0,j;
     char *str,c;
      
     scanf("%d %d",&m,&n);
     fflush(stdin);
     str=(char *)malloc(m*n*sizeof(char));
     while(i<m*n&&(c=getchar())!=EOF){
		 if(c!=' '&&c!='\n')
			 str[i++]=c;
	 }
     for(i=0;i<m;++i){
         for(j=0;j<n;++j)
             if(str[i*n+j]=='P')
                 ++step;
     }
     for(;step>0;--step){
         if(search(0,0,0,str)){
             printf("%d",step);
			 break;
		 }
	 }
     return 0;
}

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