hdu 6171---Admiral(雙向搜索)

来源:http://www.cnblogs.com/chen9510/archive/2017/08/29/7448039.html
-Advertisement-
Play Games

題目鏈接 Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battl ...


題目鏈接

 

Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.

Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.  

 

Input The first line of input contains an integer T (1 <= T <= 10), the number of test cases. 
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.  

 

Output For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.  

 

Sample Input 1 1 2 0 2 1 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5  

 

Sample Output 3   題意:有21個數由1個0、2個1、3個2、4個3、5個4和6個5組成,這21個數構成一個三角形,與楊輝三角一樣第一行1個數,第2行2個數……第6行6個數,現在要把這21個數歸位按順序擺好,0 在第一行,1在第二行……5在第六行,每次操作為0與它相鄰的數可以進行交換,同行之間不能進行交換,求最少經過多少步將所有數歸位,如果20步還我完成不了則輸出“too difficult”。   思路:搜索,但是直接搜20步會超時,所以可以從兩端各搜10步,這樣就降低了複雜度,另外可以用hash來保存狀態。   代碼如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
int dx[4]={1,1,-1,-1};
int dy[4]={0,1,-1,0};
struct Node{
    LL p[6][6];
    int r,c;
    int flag;
    int dept;
};
queue<Node>Q;
map<LL,int>M[2];
LL cal(Node a)
{
    LL ans=0;
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<=i;j++)
        {
            ans=ans*6+a.p[i][j];
        }
    }
    return ans;
}
int bfs(Node &s,Node &e)
{
    while(!Q.empty()) Q.pop();
    M[0].clear(); M[1].clear();
    M[0][cal(s)]=0;
    M[1][cal(e)]=0;
    Q.push(s);
    Q.push(e);
    while(!Q.empty())
    {
        Node x=Q.front(); Q.pop();
        LL sta=cal(x);
        if(M[!x.flag].count(sta))
        {
            int num=M[!x.flag][sta]+x.dept;
            if(num<=20) return num;
            else continue;
        }
        if(x.dept>=10) continue;
        for(int i=0;i<4;i++)
        {
            Node y=x;
            y.dept++;
            y.r+=dx[i];
            y.c+=dy[i];
            if(y.r<0 || y.r>=6 || y.c<0 || y.c>y.r) continue;
            swap(y.p[x.r][x.c],y.p[y.r][y.c]);
            if(M[y.flag].count(cal(y))==0) M[y.flag][cal(y)]=y.dept;
            Q.push(y);
        }
    }
    return -1;
}

int main()
{
    int T; cin>>T;
    Node s,e;
    while(T--)
    {
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<=i;j++)
            {
                scanf("%lld",&s.p[i][j]);
                if(s.p[i][j]==0) s.r=i, s.c=j;
                e.p[i][j]=i;
            }
        }
        s.flag=0; s.dept=0;
        e.r=0; e.c=0;
        e.flag=1; e.dept=0;
        int ans=bfs(s,e);
        if(ans>=0&&ans<=20) printf("%d\n",ans);
        else puts("too difficult");
    }
    return 0;
}
/**
1
2 1
2 0 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
*/

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在研究百度網盤的視頻解析,目的是找出網盤視頻的真實源地址。百度網盤對於超過1G的文件需要通過網盤客戶端下載,通過抓包研究,可下載源視頻有以下4個渠道: 一、https://pan.baidu.com/s/視頻地址 這是分享後的視頻地址 找到視頻源解析地址: 介面地址:https://pan.ba ...
  • 現要做一個簡單的登錄頁面,如果用戶通過驗證,會顯示Welcome用戶名的歡迎詞,反之則返回登錄頁面讓用戶再次輸入 這部分的完整代碼是JSPDemo項目里的login.jsp,下麵來分析一下關鍵代碼。 代碼位置 視頻位置 code/第3章/JSPDemo 視頻/第3章/JSP案例的講解 代碼位置 視頻 ...
  • 介面 什麼是介面 ? 介面只是定義了一些方法,而沒有去實現,多用於程式設計時,只是設計需要有什麼樣的功能,但是並沒有實現任何功能,這些功能需要被另一個類(B)繼承後,由 類B去實現其中的某個功能或全部功能。 個人的理解,多用於協作開發時,有不同的人在不同的類中實現介面中的各個方法。 在Python中 ...
  • 1.什麼是閉包(Closure)? 閉包是一個完整的設計功能模塊,可以在代碼中傳遞和使用,類似於Object-C的block(但是還是有區別,下麵會說明)或者其他語言的匿名函數(lambdas)。閉包可以捕獲或者儲存它們所在上下文的常量和變數。在Swift里等價於函數,是一等公民。 閉包有三種形式: ...
  • C++ 簡介 C++是一種靜態類型的、編譯式的、通用的、大小寫敏感的、不規則的變成語言,支持過程化編程、面向對象編程和泛型編程。被認為是一種中級語言。是C的一個超集,事實上任何合法的C程式都是合法的C++程式。 註:使用靜態類型的編程語言是在編譯時執行類型檢查,而不是在運行時執行類型檢查。 面向對象 ...
  • spring boot / cloud (七) 使用@Retryable來進行重處理 前言 什麼時候需要重處理? 在實際工作中,重處理是一個非常常見的場景,比如:發送消息失敗,調用遠程服務失敗,爭搶鎖失敗,等等,這些錯誤可能是因為網路波動造成的,等待過後重處理就能成功.通常來說,會用try/catc ...
  • 首先,在靜態頁面中,添加微信的配置文件,通過js獲取。 <script type="text/javascript"> wx.config({ debug: false, appId: '{$signPackage.appId}', timestamp: '{$signPackage.timesta ...
  • 1 電腦基礎 2 java語言概述 3 第一個java程式 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...