日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDOJ 1030 Delta-wave

發布時間:2023/12/2 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDOJ 1030 Delta-wave 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目: Problem Description A triangle field is numbered with successive integers in the way shown on the picture below.



The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route.

Write the program to determine the length of the shortest route connecting cells with numbers N and M.

Input Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).
Output Output should contain the length of the shortest route.
Sample Input 6 12
Sample Output 3
分析: 此題要求從m到n在三角形中移動,找到最短距離。主要通過找規律。 1、規定統一方向,令較小值去找較大值。 2、求行數跳轉所需的路徑長度。令較小值跳轉到較大值所在的行。 a.如果起始值的列為奇數,則可直接跳轉到下一行。 b.行跳轉的路徑 = 2 X (較大值所在行 - 較小值所在行 + 1) + 1 c.較小值所在列為偶數時比奇數多1
3、求同行中,較小值移動到較大值所需的最短路徑數。 a.較小值按照一直向斜向左下方移動,得到在行移動固定長度的移動數量的情況下的最左值。 b.同理,如果一直向斜向右下方移動,得到最右值。 c.如果較大值的列,在[列最大值, 列最右值]區間中,就判斷是否為奇數列,是長度加1,否則直接返回已有長度。 d.如果不在該區間,就計算較大值所在的列離區間的最近的長度加到ans(已經移動的長度),返回ans 代碼: #include <iostream> #include <cmath> using namespace std; int x, y; void getLocal(int s, int& rx, int& ry) {rx = (int)(ceil(sqrt(s)));int left = rx*rx - 2*rx + 2;ry = s - left + 1; } int getShortLen() {int ans = 0;int rx, cx, ry, cy;getLocal(x, rx, cx);getLocal(y, ry, cy);if(rx == ry){return abs(cx - cy);} ans += 2 * (ry - rx -1) + 1; //求出行跳轉的數量int left = cx + 1;int right = cx + 2 * (ry-rx) - 1;if(!(cx%2)) //偶數比奇數列多1{ans++;left--;right++;}if(cy >= left && cy <= right){if(cy%2){return ans+1;}else{return ans;}}else{ans += min(abs(cy-left), abs(cy-right));return ans;} } int main(void) {int temp;while(cin >> x >> y){int ans = 0;if(x > y){temp = x;x = y;y = temp;}ans = getShortLen();cout << ans << endl;}return 0; }

總結

以上是生活随笔為你收集整理的HDOJ 1030 Delta-wave的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。