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 qa6300525 at 2011-08-17 11:06:57 on Problem 1856
#include <iostream>
using namespace std;
char a[1002][1002];
int rec(int i,int j){
	if (a[i][j]=='#'&&a[i+1][j]=='#'&&a[i+1][j+1]=='#'&&a[i][j+1]=='.')
	{
		return 1;
	}
	if (a[i][j]=='#'&&a[i+1][j]=='#'&&a[i+1][j+1]=='.'&&a[i][j+1]=='#')
	{
		return 1;
	}
	if (a[i][j]=='#'&&a[i+1][j]=='.'&&a[i+1][j+1]=='#'&&a[i][j+1]=='.')
	{
		return 1;
	}
	if (a[i][j]=='#'&&a[i+1][j]=='.'&&a[i+1][j+1]=='#'&&a[i][j+1]=='.')
	{
		return 1;
	}
	if (a[i][j]=='#'&&a[i-1][j]=='.'&&a[i-1][j+1]=='#'&&a[i][j+1]=='.')
	{
		return 1;
	}
	if (a[i][j]=='#'&&a[i-1][j]=='.'&&a[i-1][j+1]=='#'&&a[i][j+1]=='#')
	{
		return 1;
	}
	if (a[i][j]=='.') return 0;
	else a[i][j]='.';
	rec(i-1,j-1);
	rec(i,j-1);
	rec(i+1,j-1);
	rec(i-1,j);
	rec(i,j);
	rec(i+1,j);
	rec(i-1,j+1);
	rec(i,j+1);
	rec(i+1,j+1);
}
int main(){
	int n,m;
	int count;
	while(cin>>n>>m){
		if (m==0&&n==0) return 0;
		for (int i=0;i<=n+1;i++) a[i][0]=a[i][m+1]='.';
		for (int i=0;i<=m+1;i++) a[0][i]=a[n+1][i]='.';
		char b[1002];
		for (int i=1;i<n+1;i++)
		{
			cin>>b;
			for(int j=1;j<m+1;j++)
				a[i][j]=b[j-1];
		}
		count=0;
		for (int i=1;i<n+1;i++)
		{
			if (count==-1) break;
			for (int j=1;j<m+1;j++)
			{
				if (a[i][j]=='#')
				{
					count++;
					rec(i,j);
					if (rec(i,j)==1)
					{
						count=-1;
						cout<<"Bad placement."<<endl;
						break;
					}
				}
			}
		}
		if (count==-1) continue;
		cout<<"There are "<<count<<" ships."<<endl;
	}
}

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