SGU - 518 Kidnapping

Berland's Police has a serious problem. A foreign ambassador arrived to Berland with an important mission, and his daughter was kidnapped just from the Royal Palace! Inspired by adventures of Erast Fandorin, the Police Chief developed the following ingenious plan. 

The ambassador agrees to pay ransom, but only if the kidnappers allow his servant to visit the girl and ensure that she is alive. The kidnappers take the blindfolded servant into a coach and transport him to the secret place, where they keep the ambassador's daughter. Certainly, the role of the servant is certainly played by a secret agent of the Police. The Police Chief knows that when the coach is moving, the wheels are creaking once on each full rotation. So, by counting the number of creaks and multiplying it by the length of the rim, one can easily calculate the distance covered by the coach. 

In spite of this brilliant idea, the affair turned to be much more difficult than it could be in a detective story. There are n intersections in the city numbered from 1 to n, some pairs of intersections are connected by bidirectional roads. The kidnappers agreed to take the "servant" to the secret place, and the servant is quite sure that this place is located at one of the intersections. Also the agent has calculated the lengths of roads between each pair of consecutive intersections on the route passed by the coach. But during the trip the agent was concentrated on counting creaks, so he could not remember in which directions the coach turned at the intersections. 

Now the route probably couldn't be restored uniquely! Moreover, the agent has a suspicion that the kidnappers could intentionally pass the same intersection or even the same road more than once to confuse the Police. 

Your task is to determine all possible locations of the secret place, given that the trip starts at the intersection number 1. 

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 200). Each of the next n lines contains n integers each. The i-th number in the j-th line l ij is the length of the road between the i-th and the j-th intersections. If l ij = 0 then the road doesn't exist. 

It is guaranteed that 0 ≤ l ij ≤ 200, l ii = 0 and l ij = l ji. The next line contains one integer k (1 ≤ k ≤ 200) — the number of roads passed by the couch. The following line contains k integers r 1r 2,..., r k (1 ≤ r i ≤ 200) — the lengths of roads between each pair of consecutive intersections on the route passed by the coach from the starting point to the secret place. 

Output
To the first line of the output write m — the number of all possible locations of the secret place. The second line should contain the numbers of intersections in increasing order separated by spaces. 

If there are no possible locations of the secret place, the output must contain the only integer 
0


Example(s)
sample input
sample output
4
0 1 2 0
1 0 1 0
2 1 0 2
0 0 2 0
3
1 1 2
3
1 3 4

題意:一個大使來訪問,結果女兒被綁架了,要確認女兒的存活,一個警察扮成大使的僕人去確認這個女兒的情況,當然大使被綁匪蒙上眼睛,然後再一個城鎮裏兜兜轉轉(所以一條路可以走好幾次),這個警察呢就可以通過聽車輪轉動的聲音來判斷兩個城市之間的距離,走過的城市其中必然存在女兒被捆綁的城市,現在給你一張圖,ij代表i城到j城的距離是這個值,然後給你一個k,接下來的k個數代表在每一步可以走的長度。從第一個城市開始走,看K步以後能確定哪些是綁匪所在的城市。

這道題可以用廣搜來解決

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int mapp[205][205];
int a[205],vis[205][205],ans[205];
struct node
{
    int x;
    int step;
};
int n,k;
void bfs()
{
    queue<node>q;
    memset(vis,0,sizeof(vis));
    memset(ans,0,sizeof(ans));
    node st,ed;
    st.x=1;
    st.step=1;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(st.step==k+1)
        {
            ans[st.x]=1;
            continue;
        }
        for(int i=1; i<=n; i++)
        {
            if(mapp[st.x][i]==a[st.step])
            {
                ed.x=i;
                ed.step=st.step+1;
                if(vis[ed.x][ed.step]==1)
                    continue;
                vis[ed.x][ed.step]=1;
                q.push(ed);
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&mapp[i][j]);
            }
        }
        scanf("%d",&k);
        for(int i=1; i<=k; i++)
            scanf("%d",&a[i]);
        bfs();
        int cnt=0,flag=0;
        for(int i=1; i<=n; i++)
        {
            if(ans[i]==1)
                cnt++;
        }
        printf("%d\n",cnt);
        for(int i=1; i<=n; i++)
        {
            if(ans[i]==1)
            {
                if(flag==0)
                {
                printf("%d",i);
                    flag++;
                }
                else
                {
                    printf(" %d",i);
                }
            }
        }
        printf("\n");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章