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   1 r   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");
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/80227671
518
sgu