"Uvalive 7364" Robots (Reverse thinking + search)

Source: Internet
Author: User
Tags bool fread

"Uvalive 7364" Robots (Reverse thinking + search)

Main topic:
n a robot on a coordinate 0~n-1.
There are two buttons that allow the robot at each location to go to the point specified by the button (∈[0,n−1] \in [0,n-1]).

Ask if you can go through a few operations to get all the robots to a point.

Considering that the final state is all robots at one point, consider the 22 robot interactions, starting with the state (i,i) to reverse the search, the state of the search (A, b) can be achieved (I,I) after several operations of a or B..

So traverse all the robot pairs, if 22 can reach the same point, eventually there must be a way all to a point.

So just bfs a little bit on the line. Starting with the DFS results tle ...

The code is as follows:

#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio > #include <climits> #include <ctime> #include <cstring> #include <queue> #include <stack&
Gt  #include <list> #include <algorithm> #include <map> #include <set> #define LL Long Long #define Pr pair<int,int> #define FREAD (CH) freopen (CH, "R", stdin) #define FWRITE (CH) freopen (CH, "w", stdout) using namespace s
td
const int INF = 0X3F3F3F3F;
const int mod = 1E9+7;
Const double EPS = 1e-8;

const int MAXN = 1123;
Vector <int> VC[2][MAXN];

BOOL VIS[MAXN][MAXN];
    void BFs (int a,int b) {queue <Pr> q;

    Q.push (Pr (b));
    Pr p;

    VIS[A][B] = Vis[b][a] = 1;
        while (!q.empty ()) {p = Q.front ();

        Q.pop ();
        A = P.first;

        b = P.second; for (int i = 0, i < 2; ++i) for (int j = 0, J < vc[i][a].size (); ++j) for (int k = 0; K &lt ;Vc[i][b].size (); ++K) if (!vis[vc[i][a][j]][vc[i][b][k]]) {VIS[VC[I][A][J]]
                        [Vc[i][b][k]] = vis[vc[i][b][k]][vc[i][a][j]] = 1;
                    Q.push (Pr (Vc[i][b][k],vc[i][a][j]));
    }}} bool solve (int n) {int x;

    for (int j = 0, J < 2; ++j) for (int i = 0; i < n; ++i) vc[j][i].clear ();
        for (int j = 0; j < 2; ++j) for (int i = 0; i < n; ++i) {scanf ("%d", &x);
    Vc[j][x].push_back (i);
    } memset (Vis,0,sizeof (VIS));

    for (int i = 0; i < n; ++i) BFS (i,i); for (int i = 0, i < n; ++i) for (int j = 0; J < N; ++j) if (!vis[i][j]) return FAL

    Se
return true;
    } int main () {//fread ("");

    Fwrite ("");

    int t,k,n;

    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &k,&n); printf ("%d%s\n", K,solve (n)?
    "YES": "NO"); } return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.