程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 3501 Escape from Enemy Territory(預處理&二分&am

poj 3501 Escape from Enemy Territory(預處理&二分&am

編輯:C++入門知識

Escape from Enemy Territory Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2301 Accepted: 637

Description

A small group of commandos has infiltrated deep into enemy territory. They have just accomplished their mission and now have to return to their rendezvous point. Of course they don’t want to get caught even if the mission is already over. Therefore they decide to take the route that will keep them as far away from any enemy base as possible.

Being well prepared for the mission, they have a detailed map of the area which marks all (known) enemy bases, their current position and the rendezvous point. For simplicity, we view the the map as a rectangular grid with integer coordinates (x, y) where 0 ≤ x < X, 0 ≤ y< Y. Furthermore, we approximate movements as horizontal and vertical steps on this grid, so we use Manhattan distance: dist((x1, y1), (x2, y2)) = |x2 ? x1| + |y2 ? y1|. The commandos can only travel in vertical and horizontal directions at each step.

Can you help them find the best route? Of course, in case that there are multiple routes that keep the same minimum distance to enemy bases, the commandos want to take a shortest route that does so. Furthermore, they don’t want to take a route off their map as it could take them in unknown, dangerous areas, but you don’t have to worry about unknown enemy bases off the map.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with three positive numbers N, X, Y. 1 ≤ N10 000 is the number of enemy bases and 1 ≤ X, Y1 000 the size of the map: coordinates x, y are on the map if 0 ≤ x < X, 0 ≤ y < Y.

One line containing two pairs of coordinates xi, yi and xr, yr: the initial position of the commandos and the rendezvous point.

N lines each containing one pair of coordinates x, y of an enemy base.

All pairs of coordinates are on the map and different from each other.

Output

Per testcase:

One line with two numbers separated by one space: the minimum separation from an enemy base and the length of the route.

Sample Input

2
1 2 2
0 0 1 1
0 1
2 5 6
0 0 4 0
2 1
2 3

Sample Output

1 2
2 14

Source

Northwestern Europe 2007

題意:

給你一張X*Y矩形地圖。上面有些點上有敵營。給你起點和終點要你找出一條最優路徑。滿足最優路徑上的點離敵營的最近最短距離是所有路徑最短的。若有多條找路徑最短的一條。

思路:

感覺有的dfs的迭代加深。通過二分來確定路徑離敵營最短距離。然後bfs來驗證。

詳細見代碼:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
int n,X,Y,sx,sy,ex,ey,mdis,head,tail,le,ri,ansd,anss;
int vis[1010][1010],dis[1010][1010];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
struct yb
{
    int x,y,sp;
} q[1010*1010];//開始隊列開小了。wa了
void prebfs()//預處理出每個地方離敵營的最短距離
{
    int i,x,y,nx,ny;
    head=0;
    tail=n;
    while(head=0&&nx=0&&ny=0&&nx=0&&ny=mdis)
            {
                vis[nx][ny]=1;
                q[tail].x=nx;
                q[tail].y=ny;
                q[tail++].sp=q[head].sp+1;
            }
        }
        head++;
    }
    return false;
}
int main()
{
    int cas,i,j,x,y;

    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d%d%d",&n,&X,&Y);
        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
        memset(vis,0,sizeof vis);
        for(i=0;i>1;
            if(bfs())
                le=mdis+1;
            else
                ri=mdis-1;
        }
        printf("%d %d\n",ansd,anss);
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved