poj 1878 Jill's Bike (广搜)

Jill's Bike
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1263 Accepted: 326 Special Judge

Description

Jill Bates hates climbing hills. Jill rides a bicycle everywhere she goes, but she always wants to go the easiest and shortest way possible. The good news is that she lives in Greenhills, which has all its roads laid out in a strictly rectangular grid--east-west roads are streets; north-south roads are avenues and the distance between any two adjacent grid points is the same. The bad news is that Greenhills is very hilly and has many one-way roads. 
In choosing a route between where she starts and where she ends, Jill has three rules: 
  1. Avoid any climb of more than 10 meters between adjacent grid points. 
  2. Never go the wrong way on a one-way road. 
  3. Always travel the shortest possible route.

Your program should help Jill find an acceptable route. 

Input

The input contains data for several maps in the following form: 
  • The first line contains two integers, separated by one or more spaces. The first integer n represents the number of streets, and the second integer m represents the number of avenues, 1<= n <=20, 1<= m <=20. 
  • The next n lines contain the altitudes of grid points. Each line represents a street and contains a sequence of m integers separated by one or more spaces. These integers represent the altitude in meters of the grid points along that street. Even if a particular street and avenue have no intersection, the altitude is still given for that grid point. 
  • One or more lines follow that define the one-way roads. Each road is represented by two pairs of integers, separated by one or more spaces, in the form: 
    street avenue street avenue

    The first street and avenue define the starting point of the road and the second pair define the ending point. Since Greenhills is a strict grid, if the two points are not adjacent in the grid, the road passes through all the intervening grid points. For example, 
    5 7 5 8 
    5 8 5 9 
    5 9 5 10

    represents roads 5-7 to 5-8, 5-8 to 5-9, and 5-9 to 5-10. Road definitions are terminated by a line containing four zeroes in the above format. 
  • Finally, one or more lines will follow that contain pairs of grid points between which Jill wants to find an optimal path, in the form: 
    street avenue street avenue

    As before, the integer pairs are separated by one or more spaces. The end of the input set is defined by a line containing four zeroes, formatted as before.

You may assume that all street and avenue numbers are within the bounds defined by the first line of input, and that all road definitions are strictly north-south or east-west. 

Output

For each path query in the input, output a sequence of grid points , from the starting grid point to the ending grid point, which meets Jill's three rules. Output grid points as 'street-avenue' separated by the word 'to'. If there is more than one path that meets Jill's criteria, any such path will be acceptable. If no route satisfies all the criteria, or if the starting and ending grid points are the same, output an appropriate message to that effect. Output a blank line between each output set.

Sample Input

3 4
10 15 20 25
19 30 35 30
10 19 26 20
1 1 1 4
2 1 2 4
3 4 3 3
3 3 1 3
1 4 3 4
2 4 2 1
1 1 2 1
0 0 0 0
1 1 2 2
2 3 2 3
2 2 1 1
0 0 0 0

Sample Output

1-1 to 1-2 to 1-3 to 1-4 to 2-4 to 2-3 to 2-2

To get from 2-3 to 2-3, stay put!

There is no acceptable route from 2-2 to 1-1.

Hint

Source

[Submit]   [Go Back]   [Status]   [Discuss]


直接上代码:

#include <iostream>
using namespace std;

const int ds[4]={-1,0,0,1};
const int da[4]={0,1,-1,0};
struct State
{
    int s,a; //(s,a)为网格的行列坐标
};
int n,m;
int alt[20][20];  //海拔高度
int s1,a1,s2,a2;//当前单项路起点和终点的坐标
int ss,as,se,ae;  //jill起点和终点的坐标
int isd[20][20][4];//是否有通向d方向的路
int par[20][20];//标记到达网格点的方向
State qu[400];//duilie
int qs,qt;

void Bfs();
void Print(int ss,int as,int se,int ae);
int Getdirection(); //求得当前单向路线的方向索引

int main()
{
    int d,i,j;
    while(cin >> n >>m)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin >> alt[i][j];
                isd[i][j][0]=isd[i][j][1]=isd[i][j][2]=isd[i][j][3]=0;
            }
        }
        while(cin >> s1 >> a1 >> s2 >>a2)
        {
            if(s1==0 && s2==0 && a1==0 && a2==0) break;
            s1--; a1--; s2--; a2--;
            d=Getdirection();
            while(s1!=s2 || a1!=a2)
            {
                isd[s1][a1][d]=1;
                s1+=ds[d]; a1+=da[d];
            }
        }
        while(cin >> ss >> as >> se >> ae)
        {
            if(ss==0 && as==0 && se==0 && ae==0)
            break;
            ss--; as--; se--; ae--;
            if(ss==se && as==ae)
            {
                cout<<"To get from "<<ss+1<<"-"<<as+1<<" to "<<se+1
                    <<"-"<<ae+1<<", stay put! ";
            }
            else
            {
                for(i=0;i<n;i++)
                {
                    for(j=0;j<m;j++)
                    par[i][j]=-1;
                }
                Bfs();
                if(par[se][ae]==-1)
                cout<<"There is no acceptable route from "<<ss+1<<"-"
                    <<as+1<<" to "<<se+1<<"-"<<ae+1<<".";
                else
                Print(ss,as,se,ae);
            }
            cout<<"\n\n";
        }
    }
    return 0;
}
void Bfs()
{
    int i;
    qu[0].s=ss; qu[0].a=as;
    qs=0;qt=1;
    while(qs<qt)
    {
        for(i=0;i<4;i++)
        {
            int cs=qu[qs].s+ds[i];
            int ca=qu[qs].a+da[i];
            if(cs<0||cs>=n||ca<0||ca>=m) continue;
            if(par[cs][ca]!=-1||isd[qu[qs].s][qu[qs].a][i]==0
               ||alt[cs][ca]-alt[qu[qs].s][qu[qs].a]>10) continue;
            par[cs][ca]=i;
            qu[qt].s=cs; qu[qt].a=ca; qt++;
        }
        qs++;
    }
}
void Print(int ss,int as,int se,int ae)
{
    if(ss!=se||as!=ae)
    {
        int cd=par[se][ae];
        Print(ss,as,se-ds[cd],ae-da[cd]);
        cout<<" to ";
    }
    cout << se+1 <<"-" << ae+1;
}
int Getdirection()
{
    if(s1==s2)
    {
        if(a1<a2) return 1;
        else return 2;
    }
    else
    {
        if(s1<s2) return 3;
        else return 0;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值