hdu 1010 dfs,奇偶剪枝

2021-06-29 00:55:48 字數 1360 閱讀 5605

背景:熟悉dfs,第一次遇見了剪枝,各種剪枝。

1.奇偶剪枝:開始一直超時,用了奇偶剪枝之後瞬間優化到312ms。

對於乙個沒有障礙的圖,起點:s(x1,y1)到終點:e(x2,y2)的理想最短路徑是:d=abs(x1-x2)+abs(y1-y2).而如果中間有障  礙物的話,路徑是在理想最短路徑上加上乙個偶數(可以證明)。這樣可以看來,任何路徑和理想最短路徑是同奇偶的。這樣就可以剪去那些不同奇偶的資料了。

2.路勁剪枝:由312ms優化到62ms,可見測試資料有多少是無用資料啊,大概是隨機生成的?

如果地圖中所有點走完了,能到達的最大步數,都小於時間t的話肯定不行。

3.極端剪枝:結果仍然是62ms,沒什麼用。

用bfs求出,起點到終點的最短路徑,如果最短路徑都大於t的話,肯定不行。

#include#include#include#include#includeusing namespace std;

char map[9][9],map1[9][9];

int n,m,ans,count,ok,direction[4][2]=,waycut;

struct placestart,end1,temp,temp1;

void dps(int x,int y)

if(map[x][y] == 'd')else

}map[x][y]='.';

count--;

}}int main(void)else if(map[i][j] == 'd')else if(map[i][j] == '.') waycut++;    //count the numbers of '.' .

map1[i][j]=map[i][j];

}getchar();

}//bfs find the shortest steps.

queueq;

q.push(start);

int q_size=1,steps=0;

while(1)

}q_size=q.size();

if(over || !q_size ) break;

}if(steps > ans) ok=2;

if(waycut < ans) ok=2;

int min_distance=abs(end1.x-start.x)+abs(end1.y-start.y);   //奇偶剪枝。

if(min_distance % 2 != ans%2) ok=2;

dps(start.x,start.y);

if(ok == 1) printf("yes\n");

else printf("no\n");

}return 0;

}

DFS專攻 HDU 1010 DFS 奇偶剪枝)

這周專攻的第一道dfs終於領會了 繼續努力!剪枝的目的就是要省時 include include include include include include include include include include include define mem a,b memset a,b,siz...

HDU1010 奇偶剪枝 DFS

第一次做剪枝的題目,剪枝,說實話研究的時間不短。好像沒什麼實質性的進展,遇到題目。絕對有會無從下手的感覺,剪枝越來越神奇了。hdu1010一道剪枝的經典題目,自己當初想用bfs過。提交了10幾遍wa,後來查了是剪枝最終死心了 ps 第一次寫剪枝題目,用了乙個模擬地圖來做奇偶性的判定條件進行剪枝,大牛...

HDU1010 奇偶剪枝 dfs)

the doggie found a bone in an ancient maze,which fascinated him a lot.however,when he picked it up,the maze began to shake,and the doggie could feel t...