Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哪位大哥指点一下,这种情况会超时吗?郁闷中~~哪位大哥指点一下,这种情况会超时吗?郁闷中~~ #include<iostream> using namespace std; int R,C; int compare_to(char *row1,char *row2); void main() { bool result; char *row1; char *row2; char *temp; int sum,res_col; int i,j; cin>>R>>C; while(R!=0 && C!=0) { row1=new char[C+1]; row2=new char[C+1]; cin>>row1; sum=0; res_col=0; for(i=0;i<C;i++) { if(row1[i]=='#') { sum++; while(row1[i+1]=='#' && i<C-1) i++; } } result=true; for(i=0;i<R-1;i++) { cin>>row2; res_col=compare_to(row1,row2); if(res_col==-1) { result=false; for(j=i+1;j<R-1;j++) { cin>>row2; } cout<<"Bad placement."<<endl; break; } sum+=res_col; temp=row2; row2=row1; row1=temp; } if(result) cout<<"There are "<<sum<<" ships."<<endl; delete []row1; delete []row2; cin>>R>>C; } } int compare_to(char *row1,char *row2) { int i,j,k; int res=0; for(i=0;i<C;i++) { if(row2[i]=='#') { j=i; while(row2[i+1]=='#' && i<C-1) { i++; } if( (j==0||row1[j-1]=='.') && (i==C-1||row1[i+1]=='.') ) { for(k=j;k<i;k++) if(row1[k] != row1[k+1]) return -1; if(row1[j]=='.') res++; } else return -1; } } return res; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator