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啊各位大虾们、、、(每次DFS时更新路径上的最大值最小值)

Posted by Debugcool at 2010-04-11 11:55:36 on Problem 2922
#include <iostream>
#include <cstring>
#define SIZE 128
using namespace std;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
int n,gmap[SIZE][SIZE];
bool vis[SIZE][SIZE];
bool legal(int x,int y)
{
    return x >= 0 && x < n && y >= 0 && y < n && !vis[x][y];
}
void DFS(int x,int y,int maxh,int minh,int dis)
{
    vis[x][y] = true;
    for (int i = 0;i < 4;i++)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!legal(nx,ny)) continue;
        int nmaxh = gmap[nx][ny] > maxh ? gmap[nx][ny] : maxh;
        int nminh = gmap[nx][ny] < minh ? gmap[nx][ny] : minh;
        if (nmaxh - nminh <= dis)
            DFS(nx,ny,nmaxh,nminh,dis);
    }
}
bool isok(int dis)
{
    memset(vis,false,sizeof(vis));
    DFS(0,0,gmap[0][0],gmap[0][0],dis);
    return vis[n-1][n-1];
}
int work()
{
    int high = 200,low = 0,mid,ans = 200;
    while (low < high)
    {
        mid = (high + low) / 2;
        if (isok(mid))
        {
            ans = mid;
            high = mid - 1;
        }
        else
            low = mid + 1;
    }
    return ans;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    for (int c = 1;c <= cas;c++)
    {
        scanf("%d",&n);
        for (int i = 0;i < n;i++)
            for (int j = 0;j < n;j++)
                scanf("%d",&gmap[i][j]);
        printf("Scenario #%d:\n%d\n\n",c,work());
    }
    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