HDU2822 Dogs

一般来说走一步不固定加上的权值的时候最好用优先队列,可以保证最优解;

做题目的时候先预先自己走一下路线,如果自己都不会走程序肯定写不出来的啦.

要好好分析走的过程,这样才可以一步到位;

 1 #include<iostream>
 2 #include<queue>
 3 #define maxn 1005
 4 int n,m;
 5 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
 6 int bx,by,ex,ey;
 7 struct node
 8 {
 9     int x,y,t;
10     friend bool operator <(node a,node b)//优先队列必须的
11     {
12         return a.t>b.t ;
13     }
14 }p,q;
15 char map[maxn][maxn];
16 bool visit[maxn][maxn];
17 using namespace std;
18 int bfs(int x,int y)
19 {
20     int i;
21     priority_queue<node> game;
22     p.x=x,p.y=y,p.t=0;
23     game.push(p);
24     visit[p.x][p.y]=true;
25     while(!game.empty())
26     {
27         q=game.top();
28         game.pop();
29         if(q.x == ex && q.y == ey) return q.t;
30         for(i=0;i<4;i++)
31         {
32          p.x=q.x+dir[i][0],p.y=q.y+dir[i][1],p.t=q.t;
33          if(p.x<0 || p.x>=n || p.y<0 || p.y>=m || visit[p.x][p.y])//淘汰不满足条件的点
34              continue;
35          visit[p.x][p.y]=true;
36          if(map[p.x][p.y]==‘.‘)  p.t=q.t+1;//经过空地需要挖坑
37         game.push(p);
38
39         }
40     }
41     return -1;
42 }
43 int main()
44 {
45     int i;
46     //freopen("2822.txt","r",stdin);
47     while(~scanf("%d%d",&n,&m),n,m)
48     {
49         memset(visit,false,sizeof(visit));
50         for(i=0;i<n;i++)
51
52             scanf("%s",map+i);//比用cin快
53
54         scanf("%d%d%d%d",&bx,&by,&ex,&ey);
55         bx--,by--,ex--,ey--;//从0读入
56         int t=bfs(bx,by);
57         printf("%d\n",t);
58     }
59     return 0;
60 }
时间: 2024-10-09 23:44:23

HDU2822 Dogs的相关文章

POJ2761 Feed the dogs

Time Limit: 6000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a sp

hdu 2822 Dogs

Dogs Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Prairie dog comes again! Someday o

XDOJ 1201: Dogs of Qwordance Senior Backend R&amp;D Engineers

XDOJ 1201: Dogs of Qwordance Senior Backend R&D Engineers 题目链接:http://acm.xidian.edu.cn/problem.php?id=1201 题目大意:已知长宽均为整数的矩形面积为$n$,现要在该矩形上划上水平和垂直间隔为相同整数的网格,问有多少种方案. 组合数 若将数$x$按素因数分解,可以得到$x=\prod_{i=0}^kp_i^{c_i}$,从而有$\tau(x)=\prod_{i=0}^k(c_i+1)$,其中$

平衡树---Feed the dogs

POJ  2761 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on

POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                            Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 20634   Accepted: 6494 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed

Poj2761-Feed the dogs(伸展树求名次)

Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line,

【POJ2761】【区间第k大】Feed the dogs(吐槽)

Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line,

POJ 2761-Feed the dogs(划分树)求区间内第k小的数

Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 17679   Accepted: 5561 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the

Splay树(区间第k小)——POJ 2761 Feed the dogs

对应POJ题目:点击打开链接 Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16655   Accepted: 5203 Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Win