搜索专题: HDU1428漫步校园
生活随笔
收集整理的這篇文章主要介紹了
搜索专题: HDU1428漫步校园
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
漫步校園
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4459????Accepted Submission(s): 1401
Problem Description LL最近沉迷于AC不能自拔,每天寢室、機房兩點一線。由于長時間坐在電腦邊,缺乏運動。他決定充分利用每次從寢室到機房的時間,在校園里散散步。整個HDU校園呈方形布局,可劃分為n*n個小方格,代表各個區域。例如LL居住的18號宿舍位于校園的西北角,即方格(1,1)代表的地方,而機房所在的第三實驗樓處于東南端的(n,n)。因有多條路線可以選擇,LL希望每次的散步路線都不一樣。另外,他考慮從A區域到B區域僅當存在一條從B到機房的路線比任何一條從A到機房的路線更近(否則可能永遠都到不了機房了…)。現在他想知道的是,所有滿足要求的路線一共有多少條。你能告訴他嗎?
Input 每組測試數據的第一行為n(2=<n<=50),接下來的n行每行有n個數,代表經過每個區域所花的時間t(0<t<=50)(由于寢室與機房均在三樓,故起點與終點也得費時)。
Output 針對每組測試數據,輸出總的路線數(小于2^63)。
Sample Input 3 1 2 3 1 2 3 1 2 3 3 1 1 1 1 1 1 1 1 1
Sample Output 1 6
Author LL
Source ACM暑期集訓隊練習賽(三)
Problem : 1428 ( 漫步校園 ) ????Judge Status : Accepted
RunId : 21258881????Language : G++????Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int N = 50 + 5; const int INF = (1<<30); long long Min[N][N],dp[N][N]; typedef struct node{ int x,y,val; node(int x=0,int y=0,int val=0):x(x),y(y),val(val){} }Node; const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int mat[N][N]; int n,newx,newy; long long DFS(int x,int y){ if(x == n-1 && y == n-1) return 1; if(dp[x][y]>0) return dp[x][y]; for(int d=0;d<4;d++){ newx = x + dir[d][0]; newy = y + dir[d][1]; if(newx >= 0 && newx < n && newy >= 0 && newy < n && Min[x][y] > Min[newx][newy]){ dp[x][y] += DFS(newx,newy); } } return dp[x][y]; } void Init(int n){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) Min[i][j] = INF; memset(dp,0,sizeof(dp)); } void BFS(){ queue <Node> Q; Node t,s; Min[n-1][n-1] = mat[n-1][n-1]; t.x = n-1,t.y = n-1,t.val = mat[n-1][n-1]; Q.push(t); while(!Q.empty()){ t = Q.front();Q.pop(); for(int d=0;d<4;d++){ newx = t.x + dir[d][0]; newy = t.y + dir[d][1]; if(newx >=0 && newx < n && newy >= 0 && newy < n){ s.val = t.val + mat[newx][newy]; if(s.val < Min[newx][newy]){ s.x = newx , s.y = newy; Min[newx][newy] = s.val; Q.push(s); } } } } } int main(){ while(scanf("%d",&n)==1){ Init(n); for(int i=0;i<n;i++) for(int j=0;j<n;j++){ scanf("%d",&mat[i][j]); } BFS(); printf("%I64d\n",DFS(0,0)); } } #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int N = 50 + 5; const int INF = (1<<30); long long Min[N][N],dp[N][N]; typedef struct node{int x,y,val;node(int x=0,int y=0,int val=0):x(x),y(y),val(val){} }Node; const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int mat[N][N]; int n,newx,newy;long long DFS(int x,int y){if(x == n-1 && y == n-1) return 1;if(dp[x][y]>0) return dp[x][y];for(int d=0;d<4;d++){newx = x + dir[d][0];newy = y + dir[d][1];if(newx >= 0 && newx < n && newy >= 0 && newy < n && Min[x][y] > Min[newx][newy]){dp[x][y] += DFS(newx,newy);}}return dp[x][y]; } void Init(int n){for(int i=0;i<n;i++)for(int j=0;j<n;j++)Min[i][j] = INF;memset(dp,0,sizeof(dp)); }void BFS(){queue <Node> Q;Node t,s;Min[n-1][n-1] = mat[n-1][n-1];t.x = n-1,t.y = n-1,t.val = mat[n-1][n-1];Q.push(t);while(!Q.empty()){t = Q.front();Q.pop();for(int d=0;d<4;d++){newx = t.x + dir[d][0];newy = t.y + dir[d][1];if(newx >=0 && newx < n && newy >= 0 && newy < n){s.val = t.val + mat[newx][newy];if(s.val < Min[newx][newy]){s.x = newx , s.y = newy;Min[newx][newy] = s.val;Q.push(s);}}}} }int main(){while(scanf("%d",&n)==1){Init(n);for(int i=0;i<n;i++)for(int j=0;j<n;j++){scanf("%d",&mat[i][j]);}BFS();printf("%I64d\n",DFS(0,0));} }
轉載于:https://www.cnblogs.com/Pretty9/p/7347713.html
總結
以上是生活随笔為你收集整理的搜索专题: HDU1428漫步校园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决WebStrom、PhpStorm等
- 下一篇: Gradle脚本基础全攻略