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

都WA 了无数次了,那位帮忙看一下

Posted by cuiaoxiang at 2005-07-28 08:56:08 on Problem 2110
#include <iostream>
using namespace std;

const int N = 100;
const int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int a[N][N];
int n;
bool flag[N][N];
int list[N * N];

inline int abs(int x)
{
	return x > 0 ? x : -x;
}

inline bool Valid(int x, int y)
{
	return x >= 0 && x < n && y >= 0 && y < n;
}

bool Check(int lim)
{
	int s, t, i, j, x, y;
	list[0] = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			flag[i][j] = false;
	flag[0][0] = true;
	for (s = 0, t = 1; s < t; s++)
	{
		for (i = 0; i < 4; i++)
		{
			x = list[s] / n + d[i][0];
			y = list[s] % n + d[i][1];
			if (Valid(x, y) && !flag[x][y] && abs(a[x][y] - a[list[s] / n][list[s] % n]) <= lim)
			{
				if (x == n - 1 && y == n - 1)
					return true;
				flag[x][y] = true;
				list[t++] = x * n + y;
			}
		}
	}
	return false;
}

int main()
{
	int i, j, up, low, mid;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
		for (j = 0;j < n; j++)
			scanf("%d", &a[i][j]);
	low = 0;
	up = 110;
	while (up > low)
	{
		mid = (up + low) / 2;
		if (Check(mid))
			up = mid;
		else low = mid + 1;
	}
	printf("%d\n", up);
	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