Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; string D = "DRUL"; const int var = 4; int trans(char c) { switch (c) { case 'R': return 0; case 'D': return 1; case 'L': return 2; case 'U': return 3; default: assert(false); } } struct ac_node { ac_node* fail; ac_node* next[var]; vector<int> ok; ac_node() : fail(nullptr), next{} {} }; ac_node* new_ac_node() { static const int pmax = 1e3; static ac_node pool[pmax]; static int it = 0; assert(it < pmax); return &pool[it++]; } ac_node* getnext(ac_node* p, char c) { while (p->next[trans(c)] == nullptr) p = p->fail; return p->next[trans(c)]; } class aho_corasick { vector<int> unite(const vector<int>& a, const vector<int>& b) { vector<int> res; set_union(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res)); return res; } int K; ac_node* root; public: aho_corasick(const vector<string>& Ts) : K(Ts.size()), root(new_ac_node()) { ac_node* now; root->fail = root; for (int i = 0; i < K; i++) { auto& T = Ts[i]; now = root; for (auto c : T) { if (now->next[trans(c)] == nullptr) { now->next[trans(c)] = new_ac_node(); } now = now->next[trans(c)]; } now->ok.push_back(i); } queue<ac_node*> q; for (int i = 0; i < var; i++) { if (root->next[i] == nullptr) { root->next[i] = root; } else { root->next[i]->fail = root; q.push(root->next[i]); } } while (!q.empty()) { now = q.front(); q.pop(); for (int i = 0; i < var; i++) { if (now->next[i] != nullptr) { ac_node* nx = now->fail; while (nx->next[i] == nullptr) { nx = nx->fail; } now->next[i]->fail = nx->next[i]; now->next[i]->ok = unite(now->next[i]->ok, nx->next[i]->ok); q.push(now->next[i]); } } } } ac_node* getroot() const { return root; } int get_node_id(ac_node* nd) const { return (int)(nd - root); } vector<int> count(const string& S) const { vector<int> res(K); ac_node* now = root; for (auto c : S) { now = getnext(now, c); for (auto k : now->ok) res[k]++; } return res; } }; using T = tuple<int, int, ac_node*>; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, m, p; while (cin >> n >> m, n | m) { vector<string> S(n); int sx = 0, sy = 0, gx = 0, gy = 0; for (int i = 0; i < n; i++) { cin >> S[i]; for (int j = 0; j < (int)S[i].size(); j++) { if (S[i][j] == 'S') { sx = i; sy = j; } else if (S[i][j] == 'G') { gx = i; gy = j; } } } cin >> p; vector<string> pts(p); for (int i = 0; i < p; i++) { cin >> pts[i]; } aho_corasick aho(pts); vector<vector<vector<int>>> f( n, vector<vector<int>>(m, vector<int>(200, INF))); queue<T> q; q.push(make_tuple(sx, sy, aho.getroot())); f[sx][sy][aho.get_node_id(aho.getroot())] = 0; while (!q.empty()) { auto tup = q.front(); q.pop(); int x = get<0>(tup), y = get<1>(tup); ac_node* nd = get<2>(tup); int id = aho.get_node_id(nd); for (int i = 0; i < 4; i++) { int tx = x + dx[i], ty = y + dy[i]; ac_node* tnd = getnext(nd, D[i]); if (0 <= tx && tx < n && 0 <= ty && ty < m && S[tx][ty] != '#' && tnd->ok.empty() && f[tx][ty][aho.get_node_id(tnd)] == INF) { q.push(make_tuple(tx, ty, tnd)); f[tx][ty][aho.get_node_id(tnd)] = f[x][y][id] + 1; } } } int res = INF; for (auto val : f[gx][gy]) { res = min(res, val); } cout << (res != INF ? res : -1) << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, pair<int, int> >, int> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 10 ? 10 : next.size()); if (dp.find(pair<string, pair<int, int> >( key, pair<int, int>(dy, dx))) != dp.end()) continue; dp[pair<string, pair<int, int> >(key, pair<int, int>(dy, dx))] = cost + 1; que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, pair<int, int> >, int> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 3 ? 3 : next.size()); if (dp.find(pair<string, pair<int, int> >( key, pair<int, int>(dy, dx))) != dp.end()) continue; dp[pair<string, pair<int, int> >(key, pair<int, int>(dy, dx))] = cost + 1; que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int H, W, K, x[100][100], dist[100][100][200], sx, sy, gx, gy, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; string S[10], dir = "DRUL"; vector<tuple<string, int, int>> vec; vector<tuple<int, int, int>> X[100][100][200]; vector<pair<int, int>> banned[200]; void build_banned() { int cnt1 = 1; vec.push_back(make_tuple("", 0, 0)); for (int i = 0; i < K; i++) { for (int j = 0; j <= S[i].size(); j++) { string F = S[i].substr(0, j); bool OK = true; for (int k = 0; k < vec.size(); k++) { if (get<0>(vec[k]) == F) OK = false; } if (OK == true && j != S[i].size()) { vec.push_back(make_tuple(F, cnt1, 0)); cnt1++; } if (OK == true && j == S[i].size()) { vec.push_back(make_tuple(F, cnt1, 1)); cnt1++; } } } for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < 4; j++) { string W2 = get<0>(vec[i]); W2 += dir[j]; bool OK = true; int to = 0; while (W2.size() >= 1) { for (int k = 0; k < vec.size(); k++) { string W3 = get<0>(vec[k]); if (W3 == W2 && get<2>(vec[k]) == 1) { OK = false; } if (W3 == W2 && get<2>(vec[k]) == 0) { if (get<0>(vec[to]).size() < W2.size()) to = k; } } W2 = W2.substr(1, W2.size() - 1); } if (OK == true) banned[i].push_back(make_pair(to, j)); } } } void add_edges() { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { for (int k = 0; k < vec.size(); k++) { if (get<2>(vec[k]) == 1) continue; for (int l = 0; l < 4; l++) { int cx = i + dx[l], cy = j + dy[l]; if (cx <= 0 || cy <= 0 || cx > H || cy > W || x[cx][cy] == 2) continue; for (int m = 0; m < banned[k].size(); m++) { if (banned[k][m].second == l) X[i][j][k].push_back(make_tuple(cx, cy, banned[k][m].first)); } } } } } } int bfs() { queue<tuple<int, int, int>> Q; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 200; k++) dist[i][j][k] = 999999999; } } Q.push(make_tuple(sx, sy, 0)); dist[sx][sy][0] = 0; while (!Q.empty()) { int a1 = get<0>(Q.front()), a2 = get<1>(Q.front()), a3 = get<2>(Q.front()); Q.pop(); for (int i = 0; i < X[a1][a2][a3].size(); i++) { int tox = get<0>(X[a1][a2][a3][i]), toy = get<1>(X[a1][a2][a3][i]), toz = get<2>(X[a1][a2][a3][i]); if (dist[tox][toy][toz] == 999999999) { dist[tox][toy][toz] = dist[a1][a2][a3] + 1; Q.push(make_tuple(tox, toy, toz)); } } } int maxn = 999999999; for (int i = 0; i < vec.size(); i++) maxn = min(maxn, dist[gx][gy][i]); return maxn; } int main() { while (true) { vec.clear(); for (int i = 0; i < 10000; i++) x[i / 100][i % 100] = 0; for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 200; k++) X[i][j][k].clear(); } } for (int i = 0; i < 200; i++) banned[i].clear(); cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { char c; cin >> c; if (c == '.') { x[i][j] = 1; } if (c == '#') { x[i][j] = 2; } if (c == 'S') { x[i][j] = 1; sx = i; sy = j; } if (c == 'G') { x[i][j] = 1; gx = i; gy = j; } } } cin >> K; for (int i = 0; i < K; i++) { cin >> S[i]; } build_banned(); add_edges(); int ans = bfs(); if (ans == 999999999) ans = -1; cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const int MAX = 50; static const int PMAX = 11; static const string DT = "URDL"; static const int di[4] = {-1, 0, 1, 0}; static const int dj[4] = {0, 1, 0, -1}; int H, W, np; char G[MAX][MAX]; string P[PMAX]; class State { public: unsigned short pos; short M[PMAX]; string path; State() { for (int i = 0; i < np; i++) M[i] = -1; path = ""; } bool shift(char ch) { path += ch; for (int i = 0; i < np; i++) { int p = -1; string s = P[i]; for (int k = 0; k < s.size(); k++) { int st = path.size() - 1 - k; if (st < 0) continue; bool match = true; for (int j = 0; j <= k; j++) { if (path[st + j] != s[j]) { match = false; break; } } if (match) p = k; } M[i] = p; if (M[i] == P[i].size() - 1) return false; } return true; } bool operator<(const State &s) const { if (pos != s.pos) return pos < s.pos; for (int i = 0; i < np; i++) { if (M[i] == s.M[i]) continue; return M[i] < s.M[i]; } return false; } }; int bfs(int si, int sj, int gi, int gj) { queue<State> Q; set<State> V; map<State, int> D; State s; s.pos = si * W + sj; D[s] = 0; V.insert(s); Q.push(s); State u, v; int ni, nj; while (!Q.empty()) { u = Q.front(); Q.pop(); if (u.pos / W == gi && u.pos % W == gj) return D[u]; for (int r = 0; r < 4; r++) { ni = u.pos / W + di[r]; nj = u.pos % W + dj[r]; if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue; if (G[ni][nj] == '#') continue; v = u; v.pos = ni * W + nj; if (!v.shift(DT[r])) continue; if (V.find(v) == V.end()) { V.insert(v); D[v] = D[u] + 1; Q.push(v); } } } return -1; } int main() { int si, sj, gi, gj; while (cin >> H >> W && H) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { si = i; sj = j; G[i][j] = '.'; } if (G[i][j] == 'G') { gi = i; gj = j; G[i][j] = '.'; } } cin >> np; for (int i = 0; i < np; i++) cin >> P[i]; cout << bfs(si, sj, gi, gj) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int m, n, nr, ns; string mat[53]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; struct Status { int x, y, s, d; Status() {} Status(int dd, int xx, int yy, int ss) : d(dd), x(xx), y(yy), s(ss) {} void Get(int &dd, int &xx, int &yy, int &ss) const { dd = d; xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return d > ts.d; } }; priority_queue<Status> pq; int Dijkstra() { while (!pq.empty()) pq.pop(); memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.emplace(0, i, j, 0); goto L; } } } L: int ans = -1; while (!pq.empty()) { int pre, x, y, s; pq.top().Get(pre, x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.emplace(pre + 1, xx, yy, ss); } } } while (1) ; return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct NODE { int x, y, a, b, cost; NODE(int A, int B, int C, int D, int E) { x = A, y = B, a = C, b = D, cost = E; } }; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; char dc[] = {'L', 'U', 'R', 'D'}; int main() { int W, H; while (cin >> H >> W) { char field[52][52]; for (int i = 0; i < 52; i++) for (int j = 0; j < 52; j++) field[i][j] = '#'; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> field[i + 1][j + 1]; int sx, sy; for (int i = 0; i < W + 1; i++) for (int j = 0; j < W + 1; j++) if (field[i][j] == 'S') sx = j, sy = i; int p; cin >> p; vector<string> P(10); for (int i = 0; i < p; i++) cin >> P[i]; for (int i = 0; i < p; i++) for (int j = 0; j < p - 1; j++) if (P[j].length() < P[j + 1].length()) swap(P[j], P[j + 1]); bool done[52][52][11][11] = {0}; queue<NODE> Q; Q.push(NODE(sx, sy, 0, 0, 0)); while (Q.size()) { NODE q = Q.front(); Q.pop(); string pt = P[q.a].substr(0, q.b); bool f = true; for (int i = 0; i < p; i++) if (~pt.find(P[i])) f = 0; if (!f) continue; if (field[q.y][q.x] == 'G') { cout << q.cost << endl; goto end; } if (done[q.y][q.x][q.a][q.b]) continue; else done[q.y][q.x][q.a][q.b] = true; if (field[q.y][q.x] == '#') continue; for (int d = 0; d < 4; d++) { string st, t = pt + dc[d]; int tx = q.x + dx[d], ty = q.y + dy[d]; for (int i = 0; i < t.size(); i++) { st = t.substr(i); for (int j = 0; j < p; j++) { if (P[j].find(st) == 0) { Q.push(NODE(q.x + dx[d], q.y + dy[d], j, st.size(), q.cost + 1)); goto ooo; } } } Q.push(NODE(q.x + dx[d], q.y + dy[d], 0, 0, q.cost + 1)); ooo:; } } cout << -1 << endl; end:; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const int MAX = 50; static const int PMAX = 11; static const string DT = "URDL"; static const int di[4] = {-1, 0, 1, 0}; static const int dj[4] = {0, 1, 0, -1}; int H, W, np; char G[MAX][MAX]; string P[PMAX]; class State { public: short pos, M[PMAX]; State() { for (int i = 0; i < np; i++) M[i] = -1; } bool shift(char ch) { bool suc = true; for (int i = 0; i < np; i++) { if (P[i][M[i] + 1] == ch) { M[i]++; if (M[i] == P[i].size() - 1) suc = false; } else { string t = P[i].substr(0, max((int)M[i], 0)) + ch; int p = -1; string s = P[i]; for (int k = 0; k < s.size(); k++) { int st = t.size() - 1 - k; if (st < 0) continue; bool match = true; for (int j = 0; j <= k; j++) { if (t[st + j] != s[j]) { match = false; break; } } if (match) p = k; } M[i] = p; } } return suc; } bool operator<(const State &s) const { if (pos != s.pos) return pos < s.pos; for (int i = 0; i < np; i++) { if (M[i] == s.M[i]) continue; return M[i] < s.M[i]; } return false; } }; int bfs(int si, int sj, int gi, int gj) { queue<State> Q; set<State> V; map<State, int> D; State s; s.pos = si * W + sj; D[s] = 0; V.insert(s); Q.push(s); State u, v; int ni, nj; while (!Q.empty()) { u = Q.front(); Q.pop(); if (u.pos / W == gi && u.pos % W == gj) return D[u]; for (int r = 0; r < 4; r++) { ni = u.pos / W + di[r]; nj = u.pos % W + dj[r]; if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue; if (G[ni][nj] == '#') continue; v = u; v.pos = ni * W + nj; if (!v.shift(DT[r])) continue; if (V.find(v) == V.end()) { V.insert(v); D[v] = D[u] + 1; Q.push(v); } } } return -1; } int main() { int si, sj, gi, gj; while (cin >> H >> W && H) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { si = i; sj = j; G[i][j] = '.'; } if (G[i][j] == 'G') { gi = i; gj = j; G[i][j] = '.'; } } cin >> np; for (int i = 0; i < np; i++) cin >> P[i]; cout << bfs(si, sj, gi, gj) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class state { public: int x, y; vector<int> ma; state(int _x, int _y, vector<int> m) { x = _x; y = _y; ma.swap(m); } bool operator<(const state& a) const { if (x != a.x) return x < a.x; if (y != a.y) return y < a.y; return ma < a.ma; } bool operator==(const state& a) const { return x == a.x && y == a.y && ma == a.ma; } }; const int dx[] = {0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int n, m, p; int sx, sy; map<state, int> msi; char field[52][52]; vector<string> ope; int N[16][16]; void initNext(const string& str, int idx) { int i, j; N[idx][0] = -1; for (i = 0, j = -1; i < str.size(); i++, j++, N[idx][i] = j) while ((j >= 0) && (str[i] != str[j])) j = N[idx][j]; } bool inside(int x, int y) { return !(x < 0 || x >= m || y >= n || y < 0 || field[y][x] == '#'); } int DIR(char c) { if (c == 'U') return 0; if (c == 'R') return 1; if (c == 'D') return 2; if (c == 'L') return 3; } int main() { while (cin >> n >> m, n | m) { ope.clear(); msi.clear(); memset(N, 0, sizeof(N)); for (int i = (0); i < (int)(n); i++) { cin >> field[i]; for (int j = (0); j < (int)(m); j++) if (field[i][j] == 'S') { sx = j; sy = i; } } cin >> p; for (int i = (0); i < (int)(p); i++) { string tmp; cin >> tmp; ope.push_back(tmp); } vector<string> tmp; for (int i = (0); i < (int)(ope.size()); i++) { int j; for (j = 0; j < ope.size(); j++) if (i != j) { if (ope[i].find(ope[j]) != string::npos) break; } if (j == ope.size()) tmp.push_back(ope[i]); } ope.swap(tmp); for (int i = (0); i < (int)(ope.size()); i++) { initNext(ope[i], i); } queue<state> q; bool goal = false; int ans = -1; q.push(state(sx, sy, vector<int>(ope.size(), 0))); msi[state(sx, sy, vector<int>(ope.size(), 0))] = 0; while (!q.empty()) { int x = q.front().x; int y = q.front().y; vector<int> ma = q.front().ma; int cost = msi[q.front()]; q.pop(); for (int k = (0); k < (int)(4); k++) { vector<int> hoge(ope.size(), 0); int tx = x + dx[k]; int ty = y + dy[k]; if (!inside(tx, ty)) continue; bool ok = true; for (int j = (0); j < (int)(ma.size()); j++) { if (DIR(ope[j][ma[j] + 1 - 1]) == k) { hoge[j] = ma[j] + 1; if (hoge[j] == ope[j].length()) { ok = false; break; } } else { if (DIR(ope[j][N[j][ma[j] + 1 - 1]]) == k) { hoge[j] = N[j][ma[j] + 1 - 1] + 1; } else { hoge[j] = (int)(DIR(ope[j][0]) == k); } } } if (!ok) continue; if (field[ty][tx] == 'G') { ans = cost + 1; goal = true; } state tmp(tx, ty, hoge); map<state, int>::iterator it = msi.lower_bound(tmp); if (it != msi.end() && it->first == tmp) { if (it->second > cost + 1) { it->second = cost + 1; q.push(tmp); } } else { msi.insert(it, make_pair(tmp, cost + 1)); q.push(tmp); } } if (goal) break; } printf("%d\n", ans); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-9; static const double PI = acos(-1.0); struct Point { int x; int y; int cost; int str; Point(int x, int y, int cost, int str) : x(x), y(y), cost(cost), str(str) { ; } }; int w, h, p; set<int> prefix; set<int> ban; set<string> pattern; char field[60][60]; char str[100]; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char move[4] = {'R', 'D', 'L', 'U'}; char revmove[300]; set<int> visit[60][60]; char temp[100]; int main() { revmove[(int)'R'] = 1; revmove[(int)'D'] = 2; revmove[(int)'L'] = 3; revmove[(int)'U'] = 4; while (scanf("%d %d", &h, &w), h | w) { int sx, sy; for (int y = 0; y < (int)(h); y++) { for (int x = 0; x < (int)(w); x++) { scanf(" %c ", &field[y][x]); if (field[y][x] == 'S') { sx = x; sy = y; } } } scanf("%d", &p); for (int i = 0; i < (int)(p); i++) { scanf("%s", str); pattern.insert((string)str); } for (set<string>::iterator it1 = pattern.begin(); it1 != pattern.end();) { for (__typeof((pattern).begin()) it2 = (pattern).begin(); it2 != (pattern).end(); it2++) { if (it1 == it2) { continue; } if (it1->find(*it2) != string::npos) { pattern.erase(it1++); goto next; } } it1++; next:; } for (__typeof((pattern).begin()) it = (pattern).begin(); it != (pattern).end(); it++) { int num = 0; for (int i = 0; i < (int)(it->size()); i++) { num = num * 5 + revmove[(int)(*it)[i]]; prefix.insert(num); } ban.insert(num); } prefix.insert(2000000000); queue<Point> que; que.push(Point(sx, sy, 0, 0)); visit[sy][sx].insert(0); while (!que.empty()) { Point p = que.front(); que.pop(); if (field[p.y][p.x] == 'G') { printf("%d\n", p.cost); return 0; } for (int i = 0; i < (int)(4); i++) { int nx = p.x + dx[i]; int ny = p.y + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h || field[ny][nx] == '#') { continue; } int nstr = p.str * 5 + i + 1; int start = pow(5, 11); while (nstr != 0 && !prefix.count(nstr)) { nstr %= start; start /= 5; } if (ban.count(nstr)) { continue; } if (visit[ny][nx].count(nstr)) { continue; } visit[ny][nx].insert(nstr); que.push(Point(nx, ny, p.cost + 1, nstr)); } } puts("-1"); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int max_n = 101; static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick() { nodes[0] = new node_t(); nodes[0]->suffLink = 0; nodes[0]->parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur]->children[c] == -1) { nodes[nodeCount] = new node_t(); nodes[nodeCount]->parent = cur; nodes[nodeCount]->charFromParent = s[i]; nodes[cur]->children[c] = nodeCount++; } cur = nodes[cur]->children[c]; } nodes[cur]->isLeaf = true; } int suffLink(int nodeIndex) { node_t *node = nodes[nodeIndex]; if (node->suffLink == -1) { node->suffLink = (node->parent == 0) ? 0 : transition(suffLink(node->parent), node->charFromParent); } return node->suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t *node = nodes[nodeIndex]; if (node->transitions[c] == -1) { node->transitions[c] = (node->children[c] != -1) ? node->children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node->transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node]->isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho; for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (dist[ny][nx][nextNode] < now.step + 1) continue; if (aho.nodes[nextNode]->isLeaf) continue; dist[ny][nx][nextNode] = now.step + 1; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct PMA { bool isRoot = false; set<int> matched; map<char, PMA*> nexts; PMA* faild; PMA() : matched(), nexts(), faild() {} ~PMA() { for (auto&& n : nexts) { delete n.second; } } static PMA build(const vector<string>& patterns) { PMA root; root.isRoot = true; root.faild = &root; for (int i = 0; i < patterns.size(); i++) { PMA* now = &root; for (int j = 0; j < patterns[i].size(); j++) { if (now->nexts.find(patterns[i][j]) == now->nexts.end()) { now->nexts[patterns[i][j]] = new PMA; } now = now->nexts[patterns[i][j]]; } now->matched.insert(i); } queue<PMA*> que; for (auto&& next : root.nexts) { next.second->faild = &root; que.push(next.second); } while (!que.empty()) { PMA* now = que.front(); que.pop(); for (auto&& next : now->nexts) { char c = next.first; PMA* nextfaild = now->faild; while (!(nextfaild->isRoot || nextfaild->nexts.find(c) != nextfaild->nexts.end())) { nextfaild = nextfaild->faild; } if (nextfaild->nexts.find(c) != nextfaild->nexts.end()) { now->nexts[c]->faild = nextfaild->nexts[c]; } else { now->nexts[c]->faild = &root; } now->nexts[c]->matched.insert(begin(nextfaild->matched), end(nextfaild->matched)); que.push(now->nexts[c]); } } return root; } tuple<set<int>, PMA*> match(const string& str, int i = 0, PMA* now_ = nullptr) { set<int> res; PMA* now = now_ ? now_ : this; for (; i < str.size(); i++) { int c = str[i]; while (!(now->isRoot || now->nexts.find(c) != now->nexts.end())) { now = now->faild; } if (now->nexts.find(c) != now->nexts.end()) { now = now->nexts[c]; } res.insert(begin(now->matched), end(now->matched)); } return make_tuple(res, now); } }; const vector<int> d4x({1, 0, -1, 0}); const vector<int> d4y({0, -1, 0, 1}); const vector<string> d4c({"D", "L", "U", "R"}); int main() { int n, m; while (cin >> n >> m, n != 0) { vector<vector<char>> bd(n, vector<char>(m)); int sx, sy, gx, gy; for (int i = 0, i_len = n; i < i_len; i++) { for (int j = 0, j_len = m; j < j_len; j++) { cin >> bd[i][j]; if (bd[i][j] == 'S') { sx = i; sy = j; } if (bd[i][j] == 'G') { gx = i; gy = j; } } } int p; cin >> p; vector<string> patterns(p); for (int i = 0, i_len = p; i < i_len; i++) { cin >> patterns[i]; } PMA pma = PMA::build(patterns); set<tuple<int, int, PMA*>> visited; queue<tuple<int, int, PMA*, int>> que; que.emplace(sx, sy, &pma, 0); int res = ([&] { while (!que.empty()) { int nx, ny, c; PMA* now; tie(nx, ny, now, c) = que.front(); que.pop(); for (int d = 0, d_len = 4; d < d_len; d++) { int tx = nx + d4x[d], ty = ny + d4y[d]; tuple<set<int>, PMA*> matched = pma.match(string(d4c[d]), 0, now); if (!(tx >= 0 && tx < n && ty >= 0 && ty < m && bd[tx][ty] != '#')) { continue; } if (!get<0>(matched).empty()) { continue; } tuple<int, int, PMA*> next(tx, ty, get<1>(matched)); if (visited.find(next) != visited.end()) { continue; } visited.insert(next); if (tx == gx && ty == gy) { return c + 1; } que.emplace(tx, ty, get<1>(matched), c + 1); } } return -1; })(); cout << res << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; namespace AhoCorasick { class Node; class SearchMachine; struct MatchingResult { map<string, int> rv; long long id; }; }; // namespace AhoCorasick class AhoCorasick::Node { private: set<string> results; map<char, AhoCorasick::Node*> transitions; vector<AhoCorasick::Node*> v_transitions; char character; AhoCorasick::Node* parent; AhoCorasick::Node* failure; public: Node() : character('\0'), parent(NULL), failure(NULL) {} Node(AhoCorasick::Node* _p, char _c) : parent(_p), character(_c), failure(NULL) {} const char get_char() const { return character; } AhoCorasick::Node* get_parent() const { return parent; } AhoCorasick::Node* get_failure() const { return failure; } void set_failure(AhoCorasick::Node* _n) { failure = _n; } AhoCorasick::Node* get_transition(char c) { if (transitions.find(c) == transitions.end()) return NULL; return transitions[c]; } const set<string>& get_results() const { return results; } void add_result(const string& str) { results.insert(str); } void add_transition(AhoCorasick::Node* node) { transitions[node->get_char()] = node; v_transitions.push_back(node); } long long get_id() const { return reinterpret_cast<long long>(this); } const vector<AhoCorasick::Node*>& get_transitions() const { return v_transitions; } }; class AhoCorasick::SearchMachine { private: set<string> keywords; AhoCorasick::Node* root; AhoCorasick::Node* state; public: SearchMachine(set<string> _k) : keywords(_k) { _build_tree(); } SearchMachine() { _build_tree(); } void _build_tree() { root = new AhoCorasick::Node(); for (set<string>::iterator it = keywords.begin(); it != keywords.end(); it++) { AhoCorasick::Node* node = root; const string& keyword = *it; for (int i = 0; i < keyword.length(); i++) { AhoCorasick::Node* next_node = node->get_transition(keyword[i]); if (next_node == NULL) { next_node = new AhoCorasick::Node(node, keyword[i]); node->add_transition(next_node); } node = next_node; } node->add_result(keyword); } vector<AhoCorasick::Node*> nodes; for (int i = 0; i < root->get_transitions().size(); i++) { root->get_transitions()[i]->set_failure(root); vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(nodes.size() + root->get_transitions()[i]->get_transitions().size()); merge(nodes.begin(), nodes.end(), root->get_transitions()[i]->get_transitions().begin(), root->get_transitions()[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); nodes.swap(tmp_nodes); } while (nodes.size() > 0) { vector<AhoCorasick::Node*> next_nodes; for (int i = 0; i < nodes.size(); i++) { AhoCorasick::Node* r = nodes[i]->get_parent()->get_failure(); char c = nodes[i]->get_char(); while ((r != NULL) && (r->get_transition(c) != NULL)) { r = r->get_failure(); } if (r == NULL) { nodes[i]->set_failure(root); } else { AhoCorasick::Node* tc = r->get_transition(c); nodes[i]->set_failure(tc); set<string> results; if (tc != NULL) results = tc->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { nodes[i]->add_result(*it); } } vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(next_nodes.size() + nodes[i]->get_transitions().size()); merge(next_nodes.begin(), next_nodes.end(), nodes[i]->get_transitions().begin(), nodes[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); next_nodes.swap(tmp_nodes); } nodes = next_nodes; } root->set_failure(root); state = root; } MatchingResult feed(const string& text) { MatchingResult mr; int index = 0; while (index < text.length()) { AhoCorasick::Node* trans = NULL; while (1) { trans = state->get_transition(text[index]); if (state == root || trans != NULL) break; state = state->get_failure(); } if (trans != NULL) { state = trans; } set<string> results = state->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { mr.rv[*it] = index - it->length() + 1; mr.id = state->get_id(); } index++; } mr.id = state->get_id(); state = root; return mr; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; set<long long> dp[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); set<string> keywords; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; keywords.insert(str); } AhoCorasick::SearchMachine* sm = new AhoCorasick::SearchMachine(keywords); priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = route + dir_str[i]; AhoCorasick::MatchingResult mr = sm->feed(next); if (dp[dy][dx].count(mr.id)) continue; dp[dy][dx].insert(mr.id); if ((!mr.rv.empty())) { continue; } long next_num = atol(next.c_str()); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct S { int t; int y, x; int h; }; int main() { for (int N, M; cin >> N >> M, N;) { char g[50][51]; int sy, sx; for (int i = 0; i < N; i++) { cin >> g[i]; for (int j = 0; j < M; j++) { if (g[i][j] == 'S') { sy = i; sx = j; } } } int P; cin >> P; string pt[10]; int pb[10] = {}; for (int i = 0; i < P; i++) { cin >> pt[i]; for (auto e : pt[i]) { pb[i] = pb[i] << 2 | e % 23 % 4; } } queue<S> que; que.push({0, sy, sx, 0}); set<int> p[50][50]; while (!que.empty()) { S cs = que.front(); if (g[cs.y][cs.x] == 'G') break; que.pop(); if (cs.t >= 9 && !p[cs.y][cs.x].insert(cs.h).second) continue; for (int i = 0; i < 4; i++) { int ny = cs.y + (~i & i - 1); int nx = cs.x + (i ? 2 - i : 0); if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') { int nh = cs.h << 2 | i; for (int j = 0; j < P; j++) { if (cs.t + 1 >= pt[j].size() && (nh & (1 << pt[j].size() * 2) - 1) == pb[j]) { goto fail; } } que.push({cs.t + 1, ny, nx, nh & (1 << 18) - 1}); } fail:; } } cout << (que.empty() ? -1 : que.front().t) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int W, H; char map[55][55]; int P; string S[15]; int sx, sy, gx, gy; vector<string> vec; int Next[105][4]; bool ng[105][4]; const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; const char c[] = {'R', 'U', 'L', 'D'}; int dist[55][55][105]; int bfs() { for (int x = 1; x <= W; x++) { for (int y = 1; y <= H; y++) { for (int k = 0; k < vec.size(); k++) { dist[x][y][k] = 1000000000; } } } dist[sx][sy][0] = 0; queue<int> Q; Q.push(((sx)*1000000 + (sy)*1000 + (0))); int x, y, state; while (Q.size()) { state = Q.front() % 1000, Q.front() /= 1000; y = Q.front() % 1000, Q.front() /= 1000; x = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i], nstate = Next[state][i]; if (ng[state][i]) continue; if (nx <= 0 || nx > W || ny <= 0 || ny > H) continue; if (map[nx][ny] == '#') continue; if (dist[nx][ny][nstate] < 1000000000) continue; dist[nx][ny][nstate] = dist[x][y][state] + 1; Q.push(((nx)*1000000 + (ny)*1000 + (nstate))); } } int ret = 1000000000; for (int i = 0; i < vec.size(); i++) ret = min(ret, dist[gx][gy][i]); if (ret == 1000000000) return -1; return ret; } int main(void) { while (1) { cin >> H >> W; if (H == 0 && W == 0) break; for (int y = 1; y <= H; y++) { for (int x = 1; x <= W; x++) { cin >> map[x][y]; if (map[x][y] == 'S') sx = x, sy = y; if (map[x][y] == 'G') gx = x, gy = y; } } cin >> P; for (int i = 0; i < P; i++) cin >> S[i]; vec.clear(); vec.push_back(""); for (int i = 0; i < P; i++) { for (int j = 0; j < S[i].size(); j++) { vec.push_back(S[i].substr(0, j)); } } sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < 4; j++) { ng[i][j] = false; } } for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < 4; j++) { string s = vec[i] + c[j]; for (int k = 0; k < P; k++) { string t = s; while (t.size()) { if (t == S[k]) { ng[i][j] = true; goto end; } t = t.substr(1); } } end:; while (1) { auto p = lower_bound(vec.begin(), vec.end(), s); if (*p == s) { Next[i][j] = p - vec.begin(); break; } s = s.substr(1); } } } cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum DIR { North, East, South, West, }; struct Node { int num, parent_id, children[4], suffix_link, depth; bool finish_FLG; DIR pattern[10]; }; struct Data { void set(int arg_row, int arg_col) { row = arg_row; col = arg_col; } int row, col; }; struct Info { Info(int arg_row, int arg_col, int arg_sum_cost, int arg_node_loc) { row = arg_row; col = arg_col; sum_cost = arg_sum_cost; node_loc = arg_node_loc; } bool operator<(const struct Info &arg) const { return sum_cost > arg.sum_cost; } int row, col, sum_cost, node_loc; }; int H, W, POW[11]; int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0}; int table[128], min_cost[50][50][200]; DIR dir_table[4] = {North, East, South, West}; char base_map[50][51]; Node nodes[200]; Data start, goal; map<int, int> MAP; bool rangeCheck(int row, int col) { if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1) return true; else { return false; } } void debug(DIR dir) { switch (dir) { case North: printf("U"); break; case East: printf("R"); break; case South: printf("D"); break; case West: printf("L"); break; } } void func() { for (int i = 0; i < 200; i++) { nodes[i].finish_FLG = false; for (int k = 0; k < 4; k++) nodes[i].children[k] = -1; } for (int row = 0; row < H; row++) { scanf("%s", base_map[row]); for (int col = 0; col < W; col++) { if (base_map[row][col] == 'S') { start.set(row, col); } else if (base_map[row][col] == 'G') { goal.set(row, col); } } } int P; scanf("%d", &P); nodes[0].parent_id = -1; nodes[0].num = 0; nodes[0].depth = 0; int node_index = 1, tmp_ch, tmp_loc, parent_id, tmp_index; char buf[11]; MAP.clear(); for (int loop = 0; loop < P; loop++) { scanf("%s", buf); tmp_index = 0; tmp_ch = table[buf[tmp_index]]; tmp_loc = 0; tmp_index = 0; while (true) { parent_id = tmp_loc; if (nodes[tmp_loc].children[tmp_ch] == -1) { nodes[tmp_loc].children[tmp_ch] = node_index++; } tmp_loc = nodes[tmp_loc].children[tmp_ch]; nodes[tmp_loc].parent_id = parent_id; for (int i = 0; i < nodes[parent_id].depth; i++) { nodes[tmp_loc].pattern[i] = nodes[parent_id].pattern[i]; } nodes[tmp_loc].pattern[nodes[parent_id].depth] = dir_table[tmp_ch]; nodes[tmp_loc].depth = nodes[parent_id].depth + 1; tmp_index++; nodes[tmp_loc].num = -1; for (int i = 0; i < nodes[tmp_loc].depth; i++) { nodes[tmp_loc].num += (nodes[tmp_loc].pattern[i] + 1) * POW[i]; } MAP[nodes[tmp_loc].num] = tmp_loc; if (buf[tmp_index] == '\0') { nodes[tmp_loc].finish_FLG = true; break; } tmp_ch = table[buf[tmp_index]]; } } int tmp, start_pos; bool Found; for (int i = 1; i < node_index; i++) { Found = false; start_pos = 1; while (start_pos < nodes[i].depth) { tmp = -1; for (int k = start_pos; k < nodes[i].depth; k++) { tmp += (nodes[i].pattern[k] + 1) * POW[k - start_pos]; } auto at = MAP.find(tmp); if (at != MAP.end()) { Found = true; break; } start_pos++; } if (!Found) { nodes[i].suffix_link = 0; } else { nodes[i].suffix_link = MAP[tmp]; } } for (int row = 0; row < H; row++) { for (int col = 0; col < W; col++) { for (int node_loc = 0; node_loc < node_index; node_loc++) min_cost[row][col][node_loc] = 2000000000; } } min_cost[start.row][start.col][0] = 0; priority_queue<Info> Q; Q.push(Info(start.row, start.col, 0, 0)); int next_node_pos, adj_row, adj_col; DIR next_dir; bool FLG; while (!Q.empty()) { if (Q.top().row == goal.row && Q.top().col == goal.col) { printf("%d\n", Q.top().sum_cost); return; } else if (Q.top().sum_cost > min_cost[Q.top().row][Q.top().col][Q.top().node_loc]) { Q.pop(); } else { for (int i = 0; i < 4; i++) { adj_row = Q.top().row + diff_row[i]; adj_col = Q.top().col + diff_col[i]; if (rangeCheck(adj_row, adj_col) == false || base_map[adj_row][adj_col] == '#') continue; switch (i) { case 0: next_dir = North; break; case 1: next_dir = West; break; case 2: next_dir = East; break; case 3: next_dir = South; break; } if (nodes[Q.top().node_loc].children[next_dir] != -1) { next_node_pos = nodes[Q.top().node_loc].children[next_dir]; } else { FLG = true; if (Q.top().node_loc == 0) { next_node_pos = 0; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push( Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } continue; } tmp = nodes[Q.top().node_loc].suffix_link; while (FLG) { if (tmp == 0) { if (nodes[0].children[next_dir] == -1) { next_node_pos = 0; } else { next_node_pos = nodes[0].children[next_dir]; } break; } else { if (nodes[tmp].pattern[nodes[tmp].depth - 1] == next_dir) { next_node_pos = tmp; break; } else { if (nodes[tmp].children[next_dir] == -1) { if (nodes[tmp].finish_FLG) { FLG = false; break; } tmp = nodes[tmp].suffix_link; } else { next_node_pos = nodes[tmp].children[next_dir]; break; } } } } if (!FLG) continue; } tmp = next_node_pos; while (tmp != 0) { if (nodes[tmp].finish_FLG) { FLG = false; break; } tmp = nodes[tmp].suffix_link; } if (!FLG) continue; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push(Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } } Q.pop(); } } printf("-1\n"); } int main() { for (int i = 0; i < 11; i++) POW[i] = pow(4, i); table['U'] = North; table['R'] = East; table['D'] = South; table['L'] = West; while (true) { scanf("%d %d", &H, &W); if (H == 0 && W == 0) break; func(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<queue> #include<cassert> #include<set> #define INF (1<<29) using namespace std; const string ds="URDL"; const int dx[4]={0,1,0,-1}; const int dy[4]={-1,0,1,0}; int n,m; vector<string>g; struct AhoCorasick{ static const int max_n =101; static const int alph_size=128; struct node_t{ int parent,suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t(){ fill(children,children+alph_size,-1); fill(transitions,transitions+alph_size,-1); suffLink=-1; isLeaf=false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick(){ nodes[0]=new node_t(); nodes[0]->suffLink=0; nodes[0]->parent=-1; nodeCount=1; } void add(string s){ int cur=0; for(int i=0;i<s.size();i++){ int c=s[i]-'A'; if(nodes[cur]->children[c]==-1){ nodes[nodeCount]=new node_t(); nodes[nodeCount]->parent=cur; nodes[nodeCount]->charFromParent=s[i]; nodes[cur]->children[c]=nodeCount++; } cur=nodes[cur]->children[c]; } nodes[cur]->isLeaf=true; } int suffLink(int nodeIndex){ node_t *node=nodes[nodeIndex]; if(node->suffLink==-1){ node->suffLink=(node->parent==0)?0:transition(suffLink(node->parent),node->charFromParent); } return node->suffLink; } int transition(int nodeIndex,char ch){ int c=ch-'A'; node_t *node=nodes[nodeIndex]; if(node->transitions[c]==-1){ node->transitions[c] = (node->children[c]!=-1)?node->children[c]:(nodeIndex==0?0:transition(suffLink(nodeIndex),ch)); } return node->transitions[c]; } int contain(string s){ int node=0,cnt=0; for(int i=0;i<s.size();i++){ node=transition(node,s[i]); cnt+=nodes[node]->isLeaf; } return cnt; } }; //AOJ 2212 struct state{int step,x,y,nodeIndex;}; int bfs(){ int p,fg=0; cin >> p; AhoCorasick aho; for(int i=0;i<p;i++){ string s; cin >> s; if(s.size()==1){ for(int j=0;j<4;j++){ fg|=(s[0]==ds[j]); } } aho.add(s); } set<int>used[51][51]; queue<state>que; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(g[i][j]=='S'){ used[i][j].insert(0); que.push((state){0,j,i,0}); break; } } } while(!que.empty()){ state now=que.front(); que.pop(); if(g[now.y][now.x]=='G')return now.step; for(int i=0;i<4;i++){ if((fg>>i)&1)continue; int nx=now.x+dx[i],ny=now.y+dy[i]; if(nx<0 || ny<0 || m<=nx || n<=ny)continue; if(g[ny][nx]=='#')continue; int nextNode=aho.transition(now.nodeIndex,ds[i]); if(aho.nodes[nextNode]->isLeaf)continue; if(used[ny][nx].find(nextNode)!=used[ny][nx].end())continue; used[ny][nx].insert(nextNode); que.push((state){now.step+1,nx,ny,nextNode}); } } return -1; } int main(void){ while(cin >> n >> m,n|m){ g.resize(n); for(int i=0;i<n;i++)cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; vector<node_t> nodes; int nodeCount; AhoCorasick(int maxNodes) { nodes.resize(maxNodes); nodes[0] = node_t(); nodes[0].suffLink = 0; nodes[0].parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur].children[c] == -1) { nodes[nodeCount] = node_t(); nodes[nodeCount].parent = cur; nodes[nodeCount].charFromParent = s[i]; nodes[cur].children[c] = nodeCount++; } cur = nodes[cur].children[c]; } nodes[cur].isLeaf = true; } int suffLink(int nodeIndex) { node_t node = nodes[nodeIndex]; if (node.suffLink == -1) { node.suffLink = (node.parent == 0) ? 0 : transition(suffLink(node.parent), node.charFromParent); } return node.suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t node = nodes[nodeIndex]; if (node.transitions[c] == -1) { node.transitions[c] = (node.children[c] != -1) ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node.transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node].isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51]; int bfs() { int p; cin >> p; AhoCorasick aho(101); for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } set<int> used[51][51]; queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { used[i][j].insert(0); que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (used[ny][nx].find(nextNode) != used[ny][nx].end()) continue; used[ny][nx].insert(nextNode); if (aho.nodes[nextNode].isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; namespace AhoCorasick { class Node; class SearchMachine; struct MatchingResult { map<string, int> rv; long long id; }; }; // namespace AhoCorasick class AhoCorasick::Node { private: set<string> results; map<char, AhoCorasick::Node*> transitions; vector<AhoCorasick::Node*> v_transitions; char character; AhoCorasick::Node* parent; AhoCorasick::Node* failure; public: Node() : character('\0'), parent(NULL), failure(NULL) {} Node(AhoCorasick::Node* _p, char _c) : parent(_p), character(_c), failure(NULL) {} const char get_char() const { return character; } AhoCorasick::Node* get_parent() const { return parent; } AhoCorasick::Node* get_failure() const { return failure; } void set_failure(AhoCorasick::Node* _n) { failure = _n; } AhoCorasick::Node* get_transition(char c) { if (transitions.find(c) == transitions.end()) return NULL; return transitions[c]; } const set<string>& get_results() const { return results; } void add_result(const string& str) { results.insert(str); } void add_transition(AhoCorasick::Node* node) { transitions[node->get_char()] = node; v_transitions.push_back(node); } long long get_id() const { return reinterpret_cast<long long>(this); } const vector<AhoCorasick::Node*>& get_transitions() const { return v_transitions; } }; class AhoCorasick::SearchMachine { private: set<string> keywords; AhoCorasick::Node* root; AhoCorasick::Node* state; public: SearchMachine(set<string> _k) : keywords(_k) { _build_tree(); } SearchMachine() { _build_tree(); } void _build_tree() { root = new AhoCorasick::Node(); for (set<string>::iterator it = keywords.begin(); it != keywords.end(); it++) { AhoCorasick::Node* node = root; const string& keyword = *it; for (int i = 0; i < keyword.length(); i++) { AhoCorasick::Node* next_node = node->get_transition(keyword[i]); if (next_node == NULL) { next_node = new AhoCorasick::Node(node, keyword[i]); node->add_transition(next_node); } node = next_node; } node->add_result(keyword); } vector<AhoCorasick::Node*> nodes; for (int i = 0; i < root->get_transitions().size(); i++) { root->get_transitions()[i]->set_failure(root); vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(nodes.size() + root->get_transitions()[i]->get_transitions().size()); merge(nodes.begin(), nodes.end(), root->get_transitions()[i]->get_transitions().begin(), root->get_transitions()[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); nodes.swap(tmp_nodes); } while (nodes.size() > 0) { vector<AhoCorasick::Node*> next_nodes; for (int i = 0; i < nodes.size(); i++) { AhoCorasick::Node* r = nodes[i]->get_parent()->get_failure(); char c = nodes[i]->get_char(); while ((r != NULL) && (r->get_transition(c) != NULL)) { r = r->get_failure(); } if (r == NULL) { nodes[i]->set_failure(root); } else { AhoCorasick::Node* tc = r->get_transition(c); nodes[i]->set_failure(tc); set<string> results; if (tc != NULL) results = tc->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { nodes[i]->add_result(*it); } } vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(next_nodes.size() + nodes[i]->get_transitions().size()); merge(next_nodes.begin(), next_nodes.end(), nodes[i]->get_transitions().begin(), nodes[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); next_nodes.swap(tmp_nodes); } nodes = next_nodes; } root->set_failure(root); state = root; } MatchingResult feed(const string& text) { MatchingResult mr; int index = 0; while (index < text.length()) { AhoCorasick::Node* trans = NULL; while (1) { trans = state->get_transition(text[index]); if (state == root || trans != NULL) break; state = state->get_failure(); } if (trans != NULL) { state = trans; } set<string> results = state->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { mr.rv[*it] = index - it->length() + 1; mr.id = state->get_id(); } index++; } mr.id = state->get_id(); state = root; return mr; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; set<long long> dp[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { dp[i][j].clear(); } } int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); set<string> keywords; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; keywords.insert(str); } AhoCorasick::SearchMachine* sm = new AhoCorasick::SearchMachine(keywords); priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = route + dir_str[i]; AhoCorasick::MatchingResult mr = sm->feed(next); if (dp[dy][dx].count(mr.id)) continue; dp[dy][dx].insert(mr.id); if ((!mr.rv.empty())) { continue; } long next_num = atol(next.c_str()); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; static char dir_idx[256]; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } ~Nodes() { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; int char2idx(char a) { return dir_idx[a]; } public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int route2num(const string& str) { int res = 0; for (int i = 0; i < str.size(); i++) { res += dir_idx[str[i]]; res *= 4; } return res / 4; } int main() { dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; set<int> dp[50][50]; Trie trie; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route.substr(0, 9); if (trie.common_prefix_search(next)) { continue; } int route_num = route2num(next); if (dp[dy][dx].count(route_num)) continue; dp[dy][dx].insert(route_num); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; char dir_idx[256]; public: Trie() { root = new Nodes(); dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } int char2idx(char a) { return dir_idx[a]; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; Trie dp[50][50]; Trie trie; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } string recent10step = next.substr(0, 10); if (dp[dy][dx].exact_match(recent10step)) continue; dp[dy][dx].insert(recent10step); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class state { public: int x, y; vector<int> ma; state(int _x, int _y, vector<int> m) { x = _x; y = _y; ma.swap(m); } bool operator<(const state& a) const { if (x != a.x) return x < a.x; if (y != a.y) return y < a.y; return ma < a.ma; } bool operator==(const state& a) const { return x == a.x && y == a.y && ma == a.ma; } }; const int dx[] = {0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int n, m, p; int sx, sy; map<state, int> msi; char field[52][52]; vector<string> ope; bool inside(int x, int y) { return !(x < 0 || x >= m || y >= n || y < 0 || field[y][x] == '#'); } int DIR(char c) { if (c == 'U') return 0; if (c == 'R') return 1; if (c == 'D') return 2; if (c == 'L') return 3; } int main() { while (cin >> n >> m, n | m) { ope.clear(); msi.clear(); for (int i = (0); i < (int)(n); i++) { cin >> field[i]; for (int j = (0); j < (int)(m); j++) if (field[i][j] == 'S') { sx = j; sy = i; } } cin >> p; for (int i = (0); i < (int)(p); i++) { string tmp; cin >> tmp; ope.push_back(tmp); } vector<string> tmp; for (int i = (0); i < (int)(ope.size()); i++) { int j; for (j = 0; j < ope.size(); j++) if (i != j) { if (ope[i].find(ope[j]) != string::npos) break; } if (j == ope.size()) tmp.push_back(ope[i]); } ope.swap(tmp); queue<state> q; bool goal = false; int ans = -1; q.push(state(sx, sy, vector<int>(ope.size(), 0))); msi[state(sx, sy, vector<int>(ope.size(), 0))] = 0; while (!q.empty()) { int x = q.front().x; int y = q.front().y; vector<int> ma = q.front().ma; int cost = msi[q.front()]; q.pop(); for (int k = (0); k < (int)(4); k++) { vector<int> hoge(ope.size(), 0); int tx = x + dx[k]; int ty = y + dy[k]; if (!inside(tx, ty)) continue; bool ok = true; for (int j = (0); j < (int)(ma.size()); j++) { if (DIR(ope[j][ma[j] + 1 - 1]) == k) { hoge[j] = ma[j] + 1; if (hoge[j] == ope[j].length()) { ok = false; break; } } else { hoge[j] = (DIR(ope[j][0]) == k); } } if (!ok) continue; if (field[ty][tx] == 'G') { ans = cost + 1; goal = true; } state tmp(tx, ty, hoge); map<state, int>::iterator it = msi.lower_bound(tmp); if (it != msi.end() && it->first == tmp) { if (it->second > cost + 1) { it->second = cost + 1; q.push(tmp); } } else { msi.insert(it, make_pair(tmp, cost + 1)); q.push(tmp); } } if (goal) break; } printf("%d\n", ans); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int max_n = 101; static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick() { nodes[0] = new node_t(); nodes[0]->suffLink = 0; nodes[0]->parent = -1; nodeCount = 1; fill(memoSuff, memoSuff + max_n, -2); fill(memoTrans[0], memoTrans[max_n], -2); } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur]->children[c] == -1) { nodes[nodeCount] = new node_t(); nodes[nodeCount]->parent = cur; nodes[nodeCount]->charFromParent = s[i]; nodes[cur]->children[c] = nodeCount++; } cur = nodes[cur]->children[c]; } nodes[cur]->isLeaf = true; } int memoSuff[max_n]; int suffLink(int nodeIndex) { if (-2 < memoSuff[nodeIndex]) return memoSuff[nodeIndex]; node_t *node = nodes[nodeIndex]; if (node->suffLink == -1) { node->suffLink = (node->parent == 0) ? 0 : transition(suffLink(node->parent), node->charFromParent); } return memoSuff[nodeIndex] = node->suffLink; } int memoTrans[max_n][alph_size]; int transition(int nodeIndex, char ch) { int c = ch - 'A'; if (-2 < memoTrans[nodeIndex][c]) return memoTrans[nodeIndex][c]; node_t *node = nodes[nodeIndex]; if (node->transitions[c] == -1) { node->transitions[c] = (node->children[c] != -1) ? node->children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return memoTrans[nodeIndex][c] = node->transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node]->isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho; for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; if (now.step > 1200 && now.step % 100 == 0) cout << now.step << endl; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (dist[ny][nx][nextNode] < now.step + 1) continue; if (aho.nodes[nextNode]->isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int _dx[] = {0, 1, 0, -1}; const int _dy[] = {-1, 0, 1, 0}; set<string> memo[50][50]; int main() { const char *ldru = "URDL"; int w, h; while (~scanf("%d%d ", &h, &w)) { char buff[64][64]; int sx, sy; int gx, gy; for (int i = 0; i < (int)(h); i++) { scanf("%s ", buff[i]); for (int j = 0; j < (int)(w); j++) { memo[i][j].clear(); if (buff[i][j] == 'S') { sx = j; sy = i; buff[i][j] = '.'; } else if (buff[i][j] == 'G') { gx = j; gy = i; buff[i][j] = '.'; } } } int n; scanf("%d ", &n); string str[n]; int mxlen = 0; for (int i = 0; i < (int)(n); i++) { char buff[20]; scanf("%s ", buff); str[i] = buff; mxlen = max(mxlen, (int)str[i].size()); } priority_queue<pair<int, pair<pair<int, int>, string> >, vector<pair<int, pair<pair<int, int>, string> > >, greater<pair<int, pair<pair<int, int>, string> > > > pq; pq.push(make_pair(0, make_pair(make_pair(sx, sy), ""))); while (pq.size()) { int cost = pq.top().first; int x = pq.top().second.first.first; int y = pq.top().second.first.second; string now = pq.top().second.second; if (x == gx && y == gy) { printf("%d\n", cost); break; } pq.pop(); if (memo[y][x].find(now) != memo[y][x].end()) continue; memo[y][x].insert(now); for (int i = 0; i < (int)(4); i++) { int xx = x + _dx[i]; int yy = y + _dy[i]; bool ok = true; if (!((((xx)) >= (0) && ((xx)) < ((w))) && (((yy)) >= (0) && ((yy)) < ((h)))) || buff[yy][xx] == '#') continue; string next; if (mxlen != 0) { if (now.size() == mxlen) next = now.substr(1) + ldru[i]; else next = now + ldru[i]; } for (int j = 0; j < (int)(n); j++) { if (next.size() >= str[j].size() && strcmp(next.c_str() + next.size() - str[j].size(), str[j].c_str()) == 0) { ok = false; break; } } if (!ok) continue; if (memo[yy][xx].find(next) != memo[yy][xx].end()) ok = false; if (ok) { pq.push(make_pair(cost + 1, make_pair(make_pair(xx, yy), next))); } } } if (pq.empty()) puts("-1"); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; namespace AhoCorasick { class Node; class SearchMachine; }; // namespace AhoCorasick class AhoCorasick::Node { private: set<string> results; map<char, AhoCorasick::Node*> transitions; vector<AhoCorasick::Node*> v_transitions; char character; AhoCorasick::Node* parent; AhoCorasick::Node* failure; public: Node() : character('\0'), parent(NULL), failure(NULL) {} Node(AhoCorasick::Node* _p, char _c) : parent(_p), character(_c), failure(NULL) {} const char get_char() const { return character; } AhoCorasick::Node* get_parent() const { return parent; } AhoCorasick::Node* get_failure() const { return failure; } void set_failure(AhoCorasick::Node* _n) { failure = _n; } AhoCorasick::Node* get_transition(char c) { if (transitions.find(c) == transitions.end()) return NULL; return transitions[c]; } const set<string>& get_results() const { return results; } void add_result(const string& str) { results.insert(str); } void add_transition(AhoCorasick::Node* node) { transitions[node->get_char()] = node; v_transitions.push_back(node); } const vector<AhoCorasick::Node*>& get_transitions() const { return v_transitions; } }; class AhoCorasick::SearchMachine { private: set<string> keywords; AhoCorasick::Node* root; AhoCorasick::Node* state; public: SearchMachine(set<string> _k) : keywords(_k) { _build_tree(); } SearchMachine() { _build_tree(); } void _build_tree() { root = new AhoCorasick::Node(); for (set<string>::iterator it = keywords.begin(); it != keywords.end(); it++) { AhoCorasick::Node* node = root; const string& keyword = *it; for (int i = 0; i < keyword.length(); i++) { AhoCorasick::Node* next_node = node->get_transition(keyword[i]); if (next_node == NULL) { next_node = new AhoCorasick::Node(node, keyword[i]); node->add_transition(next_node); } node = next_node; } node->add_result(keyword); } vector<AhoCorasick::Node*> nodes; for (int i = 0; i < root->get_transitions().size(); i++) { root->get_transitions()[i]->set_failure(root); vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(nodes.size() + root->get_transitions()[i]->get_transitions().size()); merge(nodes.begin(), nodes.end(), root->get_transitions()[i]->get_transitions().begin(), root->get_transitions()[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); nodes.swap(tmp_nodes); } while (nodes.size() > 0) { vector<AhoCorasick::Node*> next_nodes; for (int i = 0; i < nodes.size(); i++) { AhoCorasick::Node* r = nodes[i]->get_parent()->get_failure(); char c = nodes[i]->get_char(); while ((r != NULL) && (r->get_transition(c) != NULL)) { r = r->get_failure(); } if (r == NULL) { nodes[i]->set_failure(root); } else { AhoCorasick::Node* tc = r->get_transition(c); nodes[i]->set_failure(tc); set<string> results; if (tc != NULL) results = tc->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { nodes[i]->add_result(*it); } } vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(next_nodes.size() + nodes[i]->get_transitions().size()); merge(next_nodes.begin(), next_nodes.end(), nodes[i]->get_transitions().begin(), nodes[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); next_nodes.swap(tmp_nodes); } nodes = next_nodes; } root->set_failure(root); state = root; } map<string, int> feed(const string& text, map<string, int> (*callback)(int pos, const string& text)) { int index = 0; map<string, int> rv; while (index < text.length()) { AhoCorasick::Node* trans = NULL; while (1) { trans = state->get_transition(text[index]); if (state == root || trans != NULL) break; state = state->get_failure(); } if (trans != NULL) { state = trans; } set<string> results = state->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { rv = (*callback)(index - it->length() + 1, *it); if (!rv.empty()) { state = root; return rv; } } index++; } state = root; return rv; } }; class State { public: int cost; long route; int x; int y; State(int _x, int _y, int _c, long _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; map<string, int> handle_first_func(int pos, const string& keyword) { map<string, int> total; total[keyword] = pos; return total; } map<string, int> find_first(const string& text, AhoCorasick::SearchMachine* m) { map<string, int> (*handle_first)(int pos, const string& keyword) = handle_first_func; map<string, int> rv = m->feed(text, handle_first); return rv; } int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; set<long> dp[50][50]; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); set<string> keywords; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; string num_str = ""; for (int i = 0; i < str.size(); i++) { if (str[i] == 'U') { num_str += "1"; } else if (str[i] == 'R') { num_str += "2"; } else if (str[i] == 'D') { num_str += "3"; } else if (str[i] == 'L') { num_str += "4"; } } reverse(num_str.begin(), num_str.end()); keywords.insert(num_str); } AhoCorasick::SearchMachine* sm = new AhoCorasick::SearchMachine(keywords); priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); long route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); string route_str; stringstream ss; ss << route; ss >> route_str; if (route_str == "0") route_str = ""; for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string front; front = (char)((i + 1) + '0'); string next = front + route_str; next = next.substr(0, 10); if ((!find_first(next, sm).empty())) { continue; } long next_num = atol(next.c_str()); if (dp[dy][dx].count(next_num)) continue; dp[dy][dx].insert(next_num); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next_num)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } ~Nodes() { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; char dir_idx[256]; int char2idx(char a) { return dir_idx[a]; } public: Trie() { root = new Nodes(); dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; Trie dp[H][W]; Trie trie; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } string recent10step = next.substr(0, 10); if (dp[dy][dx].exact_match(recent10step)) continue; dp[dy][dx].insert(recent10step); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); dp; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct S { int y, x, t; vector<int> v; }; int main() { for (int N, M; cin >> N >> M, N;) { char g[50][51]; int y, x; for (int i = 0; i < N; i++) { cin >> g[i]; for (int j = 0; j < M; j++) { if (g[i][j] == 'S') { y = i; x = j; } } } int P; cin >> P; string p[10]; for (int i = 0; i < P; i++) { cin >> p[i]; } set<vector<int> > s[50][50]; queue<S> que; que.push({y, x, 0, vector<int>(M)}); while (!que.empty()) { auto cs = que.front(); if (g[cs.y][cs.x] == 'G') break; que.pop(); if (!s[cs.y][cs.x].insert(cs.v).second) continue; for (int i = 0; i < 4; i++) { static const int dy[] = {-1, 0, 1, 0}; static const int dx[] = {0, 1, 0, -1}; int ny = cs.y + dy[i]; int nx = cs.x + dx[i]; if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') { S ns{ny, nx, cs.t + 1, cs.v}; for (int j = 0; j < P; j++) { string c = p[j].substr(0, cs.v[j]) + "URDL"[i]; while (p[j].compare(0, c.size(), c)) { c.erase(c.begin()); } if (c == p[j]) goto fail; ns.v[j] = c.size(); } que.push(ns); fail:; } } } cout << (que.empty() ? -1 : que.front().t) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; vector<node_t> nodes; int nodeCount; AhoCorasick(int maxNodes) { nodes.resize(maxNodes); nodes[0] = node_t(); nodes[0].suffLink = 0; nodes[0].parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur].children[c] == -1) { nodes[nodeCount] = node_t(); nodes[nodeCount].parent = cur; nodes[nodeCount].charFromParent = s[i]; nodes[cur].children[c] = nodeCount++; } cur = nodes[cur].children[c]; } nodes[cur].isLeaf = true; } int suffLink(int nodeIndex) { node_t node = nodes[nodeIndex]; if (node.suffLink == -1) { node.suffLink = (node.parent == 0) ? 0 : transition(suffLink(node.parent), node.charFromParent); } return node.suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t node = nodes[nodeIndex]; if (node.transitions[c] == -1) { node.transitions[c] = (node.children[c] != -1) ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node.transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node].isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho(101); for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (dist[ny][nx][nextNode] < now.step + 1) continue; if (aho.nodes[nextNode].isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const int MAX = 50; static const int PMAX = 11; static const string DT = "URDL"; static const int di[4] = {-1, 0, 1, 0}; static const int dj[4] = {0, 1, 0, -1}; int H, W, np; char G[MAX][MAX]; string P[PMAX]; class State { public: short pos, M[PMAX]; State() { for (int i = 0; i < np; i++) M[i] = -1; } bool shift(char ch) { bool suc = true; for (int i = 0; i < np; i++) { if (P[i][M[i] + 1] == ch) { M[i]++; if (M[i] >= P[i].size() - 1) suc = false; } else { M[i] = -1; if (P[i][M[i] + 1] == ch) { M[i]++; if (M[i] >= P[i].size() - 1) suc = false; } } } return suc; } bool operator<(const State &s) const { if (pos != s.pos) return pos < s.pos; for (int i = 0; i < np; i++) { if (M[i] == s.M[i]) continue; return M[i] < s.M[i]; } return false; } }; int bfs(int si, int sj, int gi, int gj) { queue<State> Q; set<State> V; map<State, int> D; State s; s.pos = si * W + sj; D[s] = 0; V.insert(s); Q.push(s); State u, v; int ni, nj; while (!Q.empty()) { u = Q.front(); Q.pop(); if (u.pos / W == gi && u.pos % W == gj) return D[u]; for (int r = 0; r < 4; r++) { ni = u.pos / W + di[r]; nj = u.pos % W + dj[r]; if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue; if (G[ni][nj] == '#') continue; v = u; v.pos = ni * W + nj; if (!v.shift(DT[r])) continue; if (V.find(v) == V.end()) { V.insert(v); D[v] = D[u] + 1; Q.push(v); } } } return -1; } int main() { int si, sj, gi, gj; while (cin >> H >> W && H) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { si = i; sj = j; G[i][j] = '.'; } if (G[i][j] == 'G') { gi = i; gj = j; G[i][j] = '.'; } } cin >> np; for (int i = 0; i < np; i++) cin >> P[i]; cout << bfs(si, sj, gi, gj) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class state { public: int n, x, y; vector<int> route; state(int n = 0, int x = 0, int y = 0) : n(n), x(x), y(y) {} }; int dx[] = {0, 1, 0, -1}; int dy[] = {-1, 0, 1, 0}; int main() { int n, m; while (cin >> n >> m && n && m) { bool G[50][50]; int gy, gx, ans = -1; vector<int> stop[10]; set<vector<int> > s[50][50]; state u; queue<state> Q; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { G[i][j] = false; char c; cin >> c; if (c == '#') G[i][j] = true; else if (c == 'S') { u.y = i; u.x = j; } else if (c == 'G') { gy = i; gx = j; } } } int p, stop_s = 0; cin >> p; for (int i = 0; i < p; i++) { string str; cin >> str; stop_s = max(stop_s, (int)str.size()); for (int j = 0; j < str.size(); j++) { if (str[j] == 'U') stop[i].push_back(0); else if (str[j] == 'R') stop[i].push_back(1); else if (str[j] == 'D') stop[i].push_back(2); else if (str[j] == 'L') stop[i].push_back(3); } } Q.push(u); while (!Q.empty()) { u = Q.front(); Q.pop(); if (u.x == gx && u.y == gy) { ans = u.n; break; } u.n++; for (int i = 0; i < 4; i++) { state v = u; v.x += dx[i]; v.y += dy[i]; if (v.y < 0 || v.y >= n || v.x < 0 || v.x >= m) continue; v.route.push_back(i); if (v.route.size() > stop_s) v.route.erase(v.route.begin()); bool f2 = false; for (int j = 0; j < p; j++) { vector<int> vec(v.route.end() - stop[j].size(), v.route.end()); if (vec == stop[j]) { f2 = true; break; } } if (!f2 && !G[v.y][v.x] && s[v.y][v.x].find(v.route) == s[v.y][v.x].end()) { s[v.y][v.x].insert(v.route); Q.push(v); } } } cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum DIR { North, East, South, West, }; struct Node { int num, parent_id, children[4], suffix_link, depth; bool finish_FLG; DIR pattern[10]; }; struct Data { void set(int arg_row, int arg_col) { row = arg_row; col = arg_col; } int row, col; }; struct Info { Info(int arg_row, int arg_col, int arg_sum_cost, int arg_node_loc) { row = arg_row; col = arg_col; sum_cost = arg_sum_cost; node_loc = arg_node_loc; } bool operator<(const struct Info &arg) const { return sum_cost > arg.sum_cost; } int row, col, sum_cost, node_loc; }; int H, W, POW[11]; int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0}; int table[128], min_cost[50][50][200]; DIR dir_table[4] = {North, East, South, West}; char base_map[50][51]; Node nodes[200]; Data start, goal; map<int, int> MAP; bool rangeCheck(int row, int col) { if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1) return true; else { return false; } } void debug(DIR dir) { switch (dir) { case North: printf("U"); break; case East: printf("R"); break; case South: printf("D"); break; case West: printf("L"); break; } } void func() { for (int i = 0; i < 200; i++) { nodes[i].finish_FLG = false; for (int k = 0; k < 4; k++) nodes[i].children[k] = -1; } for (int row = 0; row < H; row++) { scanf("%s", base_map[row]); for (int col = 0; col < W; col++) { if (base_map[row][col] == 'S') { start.set(row, col); } else if (base_map[row][col] == 'G') { goal.set(row, col); } } } int P; scanf("%d", &P); nodes[0].parent_id = -1; nodes[0].num = 0; nodes[0].depth = 0; int node_index = 1, tmp_ch, tmp_loc, parent_id, tmp_index; char buf[11]; MAP.clear(); for (int loop = 0; loop < P; loop++) { scanf("%s", buf); tmp_index = 0; tmp_ch = table[buf[tmp_index]]; tmp_loc = 0; tmp_index = 0; while (true) { parent_id = tmp_loc; if (nodes[tmp_loc].children[tmp_ch] == -1) { nodes[tmp_loc].children[tmp_ch] = node_index++; } tmp_loc = nodes[tmp_loc].children[tmp_ch]; nodes[tmp_loc].parent_id = parent_id; for (int i = 0; i < nodes[parent_id].depth; i++) { nodes[tmp_loc].pattern[i] = nodes[parent_id].pattern[i]; } nodes[tmp_loc].pattern[nodes[parent_id].depth] = dir_table[tmp_ch]; nodes[tmp_loc].depth = nodes[parent_id].depth + 1; tmp_index++; nodes[tmp_loc].num = -1; for (int i = 0; i < nodes[tmp_loc].depth; i++) { nodes[tmp_loc].num += (nodes[tmp_loc].pattern[i] + 1) * POW[i]; } MAP[nodes[tmp_loc].num] = tmp_loc; if (buf[tmp_index] == '\0') { nodes[tmp_loc].finish_FLG = true; break; } tmp_ch = table[buf[tmp_index]]; } } int tmp, start_pos; bool Found; for (int i = 1; i < node_index; i++) { Found = false; start_pos = 1; while (start_pos < nodes[i].depth) { tmp = -1; for (int k = start_pos; k < nodes[i].depth; k++) { tmp += (nodes[i].pattern[k] + 1) * POW[k - start_pos]; } auto at = MAP.find(tmp); if (at != MAP.end()) { Found = true; break; } start_pos++; } if (!Found) { nodes[i].suffix_link = 0; } else { nodes[i].suffix_link = MAP[tmp]; } } for (int row = 0; row < H; row++) { for (int col = 0; col < W; col++) { for (int node_loc = 0; node_loc < node_index; node_loc++) min_cost[row][col][node_loc] = 2000000000; } } min_cost[start.row][start.col][0] = 0; priority_queue<Info> Q; Q.push(Info(start.row, start.col, 0, 0)); int next_node_pos, adj_row, adj_col; DIR next_dir; bool FLG; while (!Q.empty()) { if (Q.top().row == goal.row && Q.top().col == goal.col) { printf("%d\n", Q.top().sum_cost); return; } else if (Q.top().sum_cost > min_cost[Q.top().row][Q.top().col][Q.top().node_loc]) { Q.pop(); } else { for (int i = 0; i < 4; i++) { adj_row = Q.top().row + diff_row[i]; adj_col = Q.top().col + diff_col[i]; if (rangeCheck(adj_row, adj_col) == false || base_map[adj_row][adj_col] == '#') continue; switch (i) { case 0: next_dir = North; break; case 1: next_dir = West; break; case 2: next_dir = East; break; case 3: next_dir = South; break; } if (nodes[Q.top().node_loc].children[next_dir] != -1) { next_node_pos = nodes[Q.top().node_loc].children[next_dir]; } else { if (Q.top().node_loc == 0) { next_node_pos = 0; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push( Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } continue; } tmp = nodes[Q.top().node_loc].suffix_link; while (true) { if (tmp == 0) { if (nodes[0].children[next_dir] == -1) { next_node_pos = 0; } else { next_node_pos = nodes[0].children[next_dir]; } break; } else { if (nodes[tmp].pattern[nodes[tmp].depth - 1] == next_dir) { next_node_pos = tmp; break; } else { if (nodes[tmp].children[next_dir] == -1) { tmp = nodes[tmp].suffix_link; } else { next_node_pos = nodes[tmp].children[next_dir]; break; } } } } } tmp = next_node_pos; FLG = true; while (tmp != 0) { if (nodes[tmp].finish_FLG) { FLG = false; break; } tmp = nodes[tmp].suffix_link; } if (!FLG) continue; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push(Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } } Q.pop(); } } printf("-1\n"); } int main() { for (int i = 0; i < 11; i++) POW[i] = pow(4, i); table['U'] = North; table['R'] = East; table['D'] = South; table['L'] = West; while (true) { scanf("%d %d", &H, &W); if (H == 0 && W == 0) break; func(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const char dd[] = {'L', 'D', 'R', 'U'}; struct Trie { bool leaf; Trie *node[256]; Trie() { leaf = false; for (int i = 0; i < 256; i++) { node[i] = (Trie *)0; } } void insert(const string &str) { Trie *r = this; for (int i = 0; i < str.length(); i++) { char c = str[i]; if (!r->node[c]) r->node[c] = new Trie; r = r->node[c]; } r->leaf = true; } bool find(const string &str) const { const Trie *r = this; for (int i = 0; i < str.length(); i++) { char c = str[i]; if (!r->node[c]) return false; r = r->node[c]; } return r->leaf; } bool findall(const string &str) const { for (int i = 0; i < str.length(); i++) { const Trie *r = this; for (int j = i; j < str.length(); j++) { char c = str[j]; if (!r->node[c]) { r = this; if (r->node[c]) { r = r->node[c]; } } else { r = r->node[c]; if (r->leaf) return true; } } } return false; } }; struct P { int x, y; int step; int moves; string s; }; int main() { int H, W; while (cin >> H >> W, H | W) { vector<vector<bool> > wall(H, vector<bool>(W, '.')); int ix, iy, ox, oy; for (int y = 0; y < (int)(H); ++y) for (int x = 0; x < (int)(W); ++x) { char c; cin >> c; wall[y][x] = c == '#'; switch (c) { case 'S': ix = x; iy = y; break; case 'G': ox = x; oy = y; break; } } Trie trie; int p; cin >> p; for (int i = 0; i < (int)(p); ++i) { string s; cin >> s; trie.insert(s); } map<tuple<int, int, int>, bool> G; queue<P> que; que.push({ix, iy, 0, 0, ""}); while (!que.empty()) { P p = que.front(); que.pop(); int x = p.x, y = p.y; if (x == ox && y == oy) { cout << p.step << endl; goto NEXT; } for (int i = 0; i < (int)(4); ++i) { int sx = x + dx[i], sy = y + dy[i]; if (0 <= sx && sx < W && 0 <= sy && sy < H && !wall[sy][sx]) { int moves = (p.moves << 3) + i; string s = p.s + dd[i]; tuple<int, int, int> t = {sx, sy, moves}; if (!trie.findall(s) && !((G).find(t) != (G).end())) { G[t] = true; if (s.length() == 11) { s = s.substr(1, 10); } que.push({sx, sy, p.step + 1, moves, s}); } } } } cout << -1 << endl; NEXT : {} } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; class State { public: int cost; int route; char x; char y; State(char _x, char _y, int _c, int _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; bitset<500000000> dp[50][50][4]; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; string num_str = ""; for (int i = 0; i < str.size(); i++) { if (str[i] == 'U') { num_str += "1"; } else if (str[i] == 'R') { num_str += "2"; } else if (str[i] == 'D') { num_str += "3"; } else if (str[i] == 'L') { num_str += "4"; } } reverse(num_str.begin(), num_str.end()); trie.insert(num_str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); long route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); string route_str; stringstream ss; ss << route; ss >> route_str; if (route_str == "0") route_str = ""; for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string front; front = (char)((i + 1) + '0'); string next = front + route_str; next = next.substr(0, 10); if (trie.common_prefix_search(next)) { continue; } int next_num = atol(next.substr(0, 9).c_str()); if (dp[dy][dx][i][next_num]) continue; dp[dy][dx][i][next_num] = true; if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next_num)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int i, j; int h, w; while (cin >> h >> w, h || w) { int a[52][52], sx, sy, tx, ty; memset(a, -1, sizeof(a)); for (i = 1; i < h + 1; i++) { for (j = 1; j < w + 1; j++) { char s; cin >> s; if (s != '#') { a[i][j] = 0; if (0) { } else if (s == 'S') { sx = j; sy = i; } else if (s == 'G') { tx = j; ty = i; } } } } int n; cin >> n; vector<string> s; for (i = 0; i < n; i++) { string t; cin >> t; s.push_back(t); } for (i = 0; i < (int)s.size(); i++) { for (j = i + 1; j < (int)s.size(); j++) { if (s[i].find(s[j]) != string::npos) { s.erase(s.begin() + i); i--; break; } if (s[j].find(s[i]) != string::npos) { s.erase(s.begin() + j); j--; } } } int ln[10]; int mxln = 0; for (i = 0; i < (int)s.size(); i++) { ln[i] = s[i].length(); mxln = max(mxln, ln[i]); } set<string> b[52][52]; queue<pair<pair<int, int>, pair<int, string> > > c; c.push(make_pair(make_pair(sx, sy), make_pair(0, string(mxln, '*')))); while (c.empty() == 0) { int x, y, z; string t; x = c.front().first.first; y = c.front().first.second; z = c.front().second.first; t = c.front().second.second; if (x == tx && y == ty) break; c.pop(); if (b[y][x].count(t) == 0) { b[y][x].insert(t); t.erase(0, 1); if (a[y][x - 1] != -1 && b[y][x - 1].count(t + 'L') == 0) { for (i = 0; i < (int)s.size(); i++) { if (t.substr(mxln - ln[i]) + 'L' == s[i]) break; } if (i == (int)s.size()) c.push(make_pair(make_pair(x - 1, y), make_pair(z + 1, t + 'L'))); } if (a[y][x + 1] != -1 && b[y][x + 1].count(t + 'R') == 0) { for (i = 0; i < (int)s.size(); i++) { if (t.substr(mxln - ln[i]) + 'R' == s[i]) break; } if (i == (int)s.size()) c.push(make_pair(make_pair(x + 1, y), make_pair(z + 1, t + 'R'))); } if (a[y - 1][x] != -1 && b[y - 1][x].count(t + 'U') == 0) { for (i = 0; i < (int)s.size(); i++) { if (t.substr(mxln - ln[i]) + 'U' == s[i]) break; } if (i == (int)s.size()) c.push(make_pair(make_pair(x, y - 1), make_pair(z + 1, t + 'U'))); } if (a[y + 1][x] != -1 && b[y + 1][x].count(t + 'D') == 0) { for (i = 0; i < (int)s.size(); i++) { if (t.substr(mxln - ln[i]) + 'D' == s[i]) break; } if (i == (int)s.size()) c.push(make_pair(make_pair(x, y + 1), make_pair(z + 1, t + 'D'))); } } } if (c.empty()) cout << -1 << endl; else cout << c.front().second.first << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int max_n = 101; static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick() { nodes[0] = new node_t(); nodes[0]->suffLink = 0; nodes[0]->parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur]->children[c] == -1) { nodes[nodeCount] = new node_t(); nodes[nodeCount]->parent = cur; nodes[nodeCount]->charFromParent = s[i]; nodes[cur]->children[c] = nodeCount++; } cur = nodes[cur]->children[c]; } nodes[cur]->isLeaf = true; } int suffLink(int nodeIndex) { node_t *node = nodes[nodeIndex]; if (node->suffLink == -1) { node->suffLink = (node->parent == 0) ? 0 : transition(suffLink(node->parent), node->charFromParent); } return node->suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t *node = nodes[nodeIndex]; if (node->transitions[c] == -1) { node->transitions[c] = (node->children[c] != -1) ? node->children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node->transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node]->isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51]; int bfs() { int p; cin >> p; AhoCorasick aho; for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } set<int> used[51][51]; queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { used[i][j].insert(0); que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (used[ny][nx].find(nextNode) != used[ny][nx].end()) continue; used[ny][nx].insert(nextNode); if (aho.nodes[nextNode]->isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int64_t inf64 = 1e18; const double eps = 1e-9; template <typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { os << "["; for (const auto& v : vec) { os << v << ","; } os << "]"; return os; } using i64 = int64_t; const int alphabet_size = 4; class pma { public: pma *next[alphabet_size], *failure; vector<int> matches; string s; pma() { fill(next, next + alphabet_size, (pma*)0); failure = NULL; } void insert(const string& str, const int id) { insert(str, 0, id); } void insert(const string& str, const int idx, const int id) { s = str.substr(0, idx); if (idx == str.size()) { matches.push_back(id); return; } if (next[str[idx]] == NULL) this->next[str[idx]] = new pma(); next[str[idx]]->insert(str, idx + 1, id); } }; class aho_corasick { public: vector<string> dic; pma* root; aho_corasick(const vector<string>& dic_) : dic(dic_), root(new pma()) { for (int i = 0; i < dic.size(); ++i) root->insert(dic[i], i); root->failure = root; queue<pma*> que; que.push(root); while (!que.empty()) { pma* curr = que.front(); que.pop(); curr->matches.insert(curr->matches.end(), curr->failure->matches.begin(), curr->failure->matches.end()); for (int i = 0; i < alphabet_size; ++i) { pma*& next = curr->next[i]; if (next == NULL) { if (curr == root) next = root; else next = curr->failure->next[i]; continue; } if (curr == root) next->failure = root; else next->failure = curr->failure->next[i]; next->matches.insert(next->matches.end(), curr->matches.begin(), curr->matches.end()); que.push(next); } } } vector<bool> match(const string& s) const { vector<bool> res(dic.size()); const pma* state = root; for (char c : s) { state = state->next[c]; for (int i : state->matches) res[i] = true; } return res; } }; void solve(int N, int M) { vector<string> vs(N); int sy, sx, gy, gx; for (int i = (0); i < (N); ++i) { cin >> vs[i]; for (int j = (0); j < (M); ++j) { if (vs[i][j] == 'S') { sy = i; sx = j; } if (vs[i][j] == 'G') { gy = i; gx = j; } } } int P; cin >> P; vector<string> ban(P); for (int i = (0); i < (P); ++i) { cin >> ban[i]; for (char& c : ban[i]) { if (c == 'R') c = 0; if (c == 'U') c = 1; if (c == 'L') c = 2; if (c == 'D') c = 3; } } aho_corasick ac(ban); vector<int> dx = {1, 0, -1, 0}, dy = {0, -1, 0, 1}; map<pma*, int> memo[50][50]; function<int(int, int, pma*)> rec = [&](int y, int x, pma* curr) { if (memo[y][x].find(curr) != memo[y][x].end()) return memo[y][x][curr]; if (y == gy and x == gx) return memo[y][x][curr] = 0; memo[y][x][curr] = inf; for (int i = (0); i < (4); ++i) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 or N <= ny or nx < 0 or M <= nx or vs[ny][nx] == '#') continue; pma* next = curr->next[i]; if (!next->matches.empty()) continue; memo[y][x][curr] = min(memo[y][x][curr], rec(ny, nx, next) + 1); } return memo[y][x][curr]; }; int ans = rec(sy, sx, ac.root); if (ans == inf) ans = -1; cout << ans << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); for (;;) { int N, M; cin >> N >> M; if (!N and !M) break; solve(N, M); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class state { public: int x, y; vector<int> ma; state(int _x, int _y, vector<int> m) { x = _x; y = _y; ma.swap(m); } bool operator<(const state& a) const { if (x != a.x) return x < a.x; if (y != a.y) return y < a.y; return ma < a.ma; } bool operator==(const state& a) const { return x == a.x && y == a.y && ma == a.ma; } }; const int dx[] = {0, 1, 0, -1}; const int dy[] = {-1, 0, 1, 0}; int n, m, p; int sx, sy; map<state, int> msi; char field[52][52]; vector<string> ope; int N[16][16]; void initNext(const string& str, int idx) { int i, j; N[idx][0] = -1; for (i = 0, j = -1; i < str.size(); i++, j++, N[idx][i] = j) while ((j >= 0) && (str[i] != str[j])) j = N[idx][j]; } bool inside(int x, int y) { return !(x < 0 || x >= m || y >= n || y < 0 || field[y][x] == '#'); } int DIR(char c) { if (c == 'U') return 0; if (c == 'R') return 1; if (c == 'D') return 2; if (c == 'L') return 3; } int main() { while (cin >> n >> m, n | m) { ope.clear(); msi.clear(); memset(N, 0, sizeof(N)); for (int i = (0); i < (int)(n); i++) { cin >> field[i]; for (int j = (0); j < (int)(m); j++) if (field[i][j] == 'S') { sx = j; sy = i; } } cin >> p; for (int i = (0); i < (int)(p); i++) { string tmp; cin >> tmp; ope.push_back(tmp); } vector<string> tmp; for (int i = (0); i < (int)(ope.size()); i++) { int j; for (j = 0; j < ope.size(); j++) if (i != j) { if (ope[i].find(ope[j]) != string::npos) break; } if (j == ope.size()) tmp.push_back(ope[i]); } ope.swap(tmp); for (int i = (0); i < (int)(ope.size()); i++) { initNext(ope[i], i); } queue<state> q; bool goal = false; int ans = -1; q.push(state(sx, sy, vector<int>(ope.size(), 0))); msi[state(sx, sy, vector<int>(ope.size(), 0))] = 0; while (!q.empty()) { int x = q.front().x; int y = q.front().y; vector<int> ma = q.front().ma; int cost = msi[q.front()]; q.pop(); for (int k = (0); k < (int)(4); k++) { vector<int> hoge(ope.size(), 0); int tx = x + dx[k]; int ty = y + dy[k]; if (!inside(tx, ty)) continue; bool ok = true; for (int j = (0); j < (int)(ma.size()); j++) { if (DIR(ope[j][ma[j] + 1 - 1]) == k) { hoge[j] = ma[j] + 1; if (hoge[j] == ope[j].length()) { ok = false; break; } } else { string aaa = ope[j].substr(0, N[j][ma[j] + 1 - 1]) + string(1, k == 0 ? 'U' : k == 1 ? 'R' : k == 2 ? 'D' : 'L'); int l; for (l = 0; l < aaa.size(); l++) { int u; for (u = 0; u + l < aaa.size(); u++) { if (aaa[l + u] != ope[j][u]) break; } if (u + l == aaa.size()) { hoge[j] = aaa.size() - l; break; } } if (l == aaa.size()) { hoge[j] = 0; } } } if (!ok) continue; if (field[ty][tx] == 'G') { ans = cost + 1; goal = true; } state tmp(tx, ty, hoge); map<state, int>::iterator it = msi.lower_bound(tmp); if (it != msi.end() && it->first == tmp) { if (it->second > cost + 1) { it->second = cost + 1; q.push(tmp); } } else { msi.insert(it, make_pair(tmp, cost + 1)); q.push(tmp); } } if (goal) break; } printf("%d\n", ans); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class Sit { public: int x, y; string cur; }; class AhoCorasick { private: static const int MAX_V = 1000001; private: map<string, int> _dict; int _vertex; map<string, set<string> > _stepPrv; map<string, string> _stepBack; set<string> _dictStr; public: AhoCorasick(const vector<string> inStrings) : _vertex(1) { for (int i = 0; i < inStrings.size(); i++) _dictStr.insert(inStrings[i]); makeTree(inStrings); } int getVetex() { return _vertex; } bool isMatchOneByOne(char ch, string &cur) { while (1) { string nxt = cur; nxt += ch; if (_stepPrv.find(cur) != _stepPrv.end() && _stepPrv[cur].find(nxt) != _stepPrv[cur].end()) { cur = nxt; break; } else { if (cur == "") return false; else if (_stepBack.find(cur) == _stepBack.end()) cur = ""; else { cur = _stepBack[cur]; } } } if (_dictStr.find(cur) != _dictStr.end()) return true; return false; } bool isMatchLeastOne(const string &s) { string cur = ""; for (int i = 0; i < s.size(); i++) { string nxt = cur + s[i]; if (_stepPrv.find(cur) != _stepPrv.end() && _stepPrv[cur].find(nxt) != _stepPrv[cur].end()) cur = nxt; else { if (cur == "") continue; else if (_stepBack.find(cur) == _stepBack.end()) cur = ""; else { i--; cur = _stepBack[cur]; } } if (_dictStr.find(cur) != _dictStr.end()) return true; } return false; } private: AhoCorasick(const AhoCorasick &ah); AhoCorasick &operator=(const AhoCorasick &ah); private: void makeTree(const vector<string> &inStrings) { _dict[""] = 0; for (int i = 0; i < (int)inStrings.size(); i++) { for (int j = 0; j < (int)inStrings[i].size(); j++) { string s = inStrings[i].substr(0, j + 1); if (_dict.find(s) == _dict.end()) _dict[s] = _vertex++; } } for (map<string, int>::iterator it = _dict.begin(); it != _dict.end(); it++) { for (map<string, int>::iterator iit = _dict.begin(); iit != _dict.end(); iit++) { if (it == iit) continue; if (it->first.size() == iit->first.size() - 1 && it->first == iit->first.substr(0, it->first.size())) { _stepPrv[it->first].insert(iit->first); } } } for (map<string, int>::iterator it = _dict.begin(); it != _dict.end(); it++) { if (it->first == "") continue; bool ok = false; for (int i = 0; i < it->first.size() - 1; i++) { string s = it->first.substr(i + 1); if (_dict.find(s) != _dict.end()) { _stepBack[it->first] = s; ok = true; break; } } if (!ok) _stepBack[it->first] = ""; } } }; int h, w; int n; char field[51][51]; string prohibits[20]; int sy, sx, gx, gy; set<string> used[51][51]; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; int bfs() { vector<string> vs; for (int i = 0; i < n; i++) vs.push_back(prohibits[i]); AhoCorasick ac(vs); Sit init; init.x = sx; init.y = sy; queue<Sit> q[2]; int cur = 0; int nxt = 1; q[cur].push(init); used[init.y][init.x].insert(""); int cnt = 0; while (q[cur].size()) { while (q[cur].size()) { Sit s = q[cur].front(); q[cur].pop(); if (s.y == gy && s.x == gx) return cnt; for (int i = 0; i < 4; i++) { int ny = s.y + dy[i]; int nx = s.x + dx[i]; if (ny >= 0 && nx >= 0 && ny < h && nx < w && field[ny][nx] == '.') { char ch = i + '0'; Sit nsit; nsit.y = ny; nsit.x = nx; nsit.cur = s.cur; bool b = ac.isMatchOneByOne(ch, nsit.cur); if (b) continue; if (used[ny][nx].find(nsit.cur) == used[ny][nx].end()) { q[nxt].push(nsit); used[ny][nx].insert(nsit.cur); } } } } cnt++; swap(cur, nxt); } return -1; } int main() { while (cin >> h >> w && (h | w)) { for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) used[i][j].clear(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> field[i][j]; if (field[i][j] == 'S') { sy = i; sx = j; field[i][j] = '.'; } else if (field[i][j] == 'G') { gy = i; gx = j; field[i][j] = '.'; } } } cin >> n; for (int i = 0; i < n; i++) { cin >> prohibits[i]; for (int j = 0; j < (int)prohibits[i].size(); j++) { if (prohibits[i][j] == 'U') prohibits[i][j] = '0'; else if (prohibits[i][j] == 'R') prohibits[i][j] = '1'; else if (prohibits[i][j] == 'D') prohibits[i][j] = '2'; else if (prohibits[i][j] == 'L') prohibits[i][j] = '3'; } } int res = bfs(); cout << res << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int NODE_NUM; struct Node { map<char, Node*> next; Node* fail; vector<int> match; int node_id; Node() : fail(NULL) { node_id = NODE_NUM++; } ~Node() { for (auto p : next) if (p.second) delete p.second; } }; Node* build(vector<string> pattens) { Node* root = new Node(); root->fail = root; for (int i = 0; i < pattens.size(); i++) { Node* p = root; for (auto c : pattens[i]) { if (p->next[c] == 0) p->next[c] = new Node(); p = p->next[c]; } p->match.push_back(i); } queue<Node*> que; for (int i = 0; i < 128; i++) { if (!root->next[i]) { root->next[i] = root; } else { root->next[i]->fail = root; que.push(root->next[i]); } } while (!que.empty()) { Node* p = que.front(); que.pop(); for (int i = 0; i < 128; i++) if (p->next[i]) { Node* np = p->next[i]; que.push(np); Node* f = p->fail; while (!f->next[i]) f = f->fail; np->fail = f->next[i]; np->match.insert(np->match.end(), np->fail->match.begin(), np->fail->match.end()); } } return root; } Node* next_node(Node* p, char c) { while (!p->next[c]) p = p->fail; return p->next[c]; } vector<pair<int, int> > match(Node* root, string query) { int n = query.size(); vector<pair<int, int> > res; Node* p = root; for (int i = 0; i < (int)(n); ++i) { int c = query[i]; p = next_node(p, c); for (int k : p->match) { res.push_back(pair<int, int>(i, k)); } } return res; } struct State { int x, y; Node* p; }; int main() { const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char dc[4] = {'R', 'D', 'L', 'U'}; int H, W; while (cin >> H >> W && H > 0) { vector<string> grid(H); for (int y = 0; y < (int)(H); ++y) cin >> grid[y]; int sx, sy, gx, gy; for (int y = 0; y < (int)(H); ++y) for (int x = 0; x < (int)(W); ++x) if (grid[y][x] == 'S') sx = x, sy = y, grid[y][x] = '.'; for (int y = 0; y < (int)(H); ++y) for (int x = 0; x < (int)(W); ++x) if (grid[y][x] == 'G') gx = x, gy = y, grid[y][x] = '.'; int PN; cin >> PN; vector<string> pat(PN); for (int i = 0; i < (int)(PN); ++i) cin >> pat[i]; NODE_NUM = 0; Node* root = build(pat); assert(NODE_NUM <= 110); int dist[50][50][110] = {}; for (int y = 0; y < (int)(H); ++y) for (int x = 0; x < (int)(W); ++x) for (int i = 0; i < (int)(NODE_NUM); ++i) dist[y][x][i] = INT_MAX; queue<State> que; que.push({sx, sy, root}); dist[sy][sx][root->node_id] = 0; while (!que.empty()) { State s = que.front(); que.pop(); int d = dist[s.y][s.x][s.p->node_id]; for (int r = 0; r < (int)(4); ++r) { int nx = s.x + dx[r]; int ny = s.y + dy[r]; if (0 <= nx && 0 <= ny && nx < W && ny < H) { if (grid[ny][nx] == '#') continue; Node* np = next_node(s.p, dc[r]); if (np->match.size() > 0) continue; if (dist[ny][nx][np->node_id] > d + 1) { dist[ny][nx][np->node_id] = d + 1; que.push({nx, ny, np}); } } } } int ans = INT_MAX; for (int i = 0; i < (int)(NODE_NUM); ++i) ans = min(ans, dist[gy][gx][i]); if (ans == INT_MAX) ans = -1; cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> #include <string> #include <iostream> #include <set> #include <map> #include <time.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; enum DIR{ North, East, South, West, }; struct Node{ int num,parent_id,children[4],suffix_link,depth; bool finish_FLG; DIR pattern[10]; }; struct Data{ void set(int arg_row,int arg_col){ row = arg_row; col = arg_col; } int row,col; }; struct Info{ Info(int arg_row,int arg_col,int arg_sum_cost,int arg_node_loc){ row = arg_row; col = arg_col; sum_cost = arg_sum_cost; node_loc = arg_node_loc; } bool operator<(const struct Info &arg) const{ return sum_cost > arg.sum_cost; } int row,col,sum_cost,node_loc; }; int H,W,POW[11]; int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0}; int table[128],min_cost[50][50][200]; DIR dir_table[4] = {North,East,South,West}; char base_map[50][51]; Node nodes[200]; Data start,goal; map<int,int> MAP; bool rangeCheck(int row,int col){ if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true; else{ return false; } } void debug(DIR dir){ switch(dir){ case North: printf("U"); break; case East: printf("R"); break; case South: printf("D"); break; case West: printf("L"); break; } } void func(){ for(int i = 0; i < 200; i++){ nodes[i].finish_FLG = false; for(int k = 0; k < 4; k++)nodes[i].children[k] = -1; } for(int row = 0; row < H; row++){ scanf("%s",base_map[row]); for(int col = 0; col < W; col++){ if(base_map[row][col] == 'S'){ start.set(row,col); }else if(base_map[row][col] == 'G'){ goal.set(row,col); } } } int P; scanf("%d",&P); nodes[0].parent_id = -1; nodes[0].num = 0; nodes[0].depth = 0; int node_index = 1,tmp_ch,tmp_loc,parent_id,tmp_index; char buf[11]; MAP.clear(); //トライ木を構成する for(int loop = 0; loop < P; loop++){ //printf("\nloop:%d\n",loop); scanf("%s",buf); tmp_index = 0; tmp_ch = table[buf[tmp_index]]; tmp_loc = 0; tmp_index = 0; while(true){ parent_id = tmp_loc; if(nodes[tmp_loc].children[tmp_ch] == -1){ nodes[tmp_loc].children[tmp_ch] = node_index++; } //小ノードに移動 tmp_loc = nodes[tmp_loc].children[tmp_ch]; /* printf("tmp_loc:%d\n",tmp_loc); debug(dir_table[tmp_ch]); printf("\n");*/ nodes[tmp_loc].parent_id = parent_id; //文字列をコピー&追加 for(int i = 0; i < nodes[parent_id].depth; i++){ nodes[tmp_loc].pattern[i] = nodes[parent_id].pattern[i]; } nodes[tmp_loc].pattern[nodes[parent_id].depth] = dir_table[tmp_ch]; nodes[tmp_loc].depth = nodes[parent_id].depth+1; tmp_index++; //配列をコード化 nodes[tmp_loc].num = -1; for(int i = 0; i < nodes[tmp_loc].depth; i++){ nodes[tmp_loc].num += (nodes[tmp_loc].pattern[i]+1)*POW[i]; //★ハマりpoint★ } //printf("num:%d\n",nodes[tmp_loc].num); MAP[nodes[tmp_loc].num] = tmp_loc; if(buf[tmp_index] == '\0'){ nodes[tmp_loc].finish_FLG = true; //printf("%dにてfinish\n",tmp_loc); break; } tmp_ch = table[buf[tmp_index]]; } } int tmp,start_pos; bool Found; //printf("\n"); //サフィックスリンクを張る for(int i = 1; i < node_index; i++){ /*printf("nodes[%d] ",i); for(int k = 0; k < nodes[i].depth; k++){ debug(nodes[i].pattern[k]); } printf("\n");*/ Found = false; start_pos = 1; //遷移できる場所を探す while(start_pos < nodes[i].depth){ //printf("start_pos:%d\n",start_pos); tmp = -1; for(int k = start_pos; k < nodes[i].depth; k++){ tmp += (nodes[i].pattern[k]+1)*POW[k-start_pos]; } //printf("tmp:%d\n",tmp); auto at = MAP.find(tmp); if(at != MAP.end()){ Found = true; break; } start_pos++; } if(!Found){ //接続できるノードがないので、ルートノードに貼る //printf("原点にsuffix_linkを張った\n"); nodes[i].suffix_link = 0; }else{ //printf("%dにsuffix_linkを張った\n",MAP[tmp]); nodes[i].suffix_link = MAP[tmp]; } } for(int row = 0; row < H; row++){ for(int col = 0; col < W; col++){ for(int node_loc = 0; node_loc < node_index; node_loc++)min_cost[row][col][node_loc] = BIG_NUM; } } min_cost[start.row][start.col][0] = 0; priority_queue<Info> Q; Q.push(Info(start.row,start.col,0,0)); int next_node_pos,adj_row,adj_col; DIR next_dir; bool FLG; int calc; while(!Q.empty()){ if(Q.top().row == goal.row && Q.top().col == goal.col){ printf("%d\n",Q.top().sum_cost); //Qをコストの小さい順に回しているから、最速でゴールするデータは最速で遷移してくるはず return; }else if(Q.top().sum_cost > min_cost[Q.top().row][Q.top().col][Q.top().node_loc]){ Q.pop(); }else{ for(int i = 0; i < 4; i++){ adj_row = Q.top().row+diff_row[i]; adj_col = Q.top().col+diff_col[i]; if(rangeCheck(adj_row,adj_col) == false || base_map[adj_row][adj_col] == '#')continue; switch(i){ case 0: next_dir = North; break; case 1: next_dir = West; break; case 2: next_dir = East; break; case 3: next_dir = South; break; } //printf("現在:(%d,%d) next:(%d,%d)\n",Q.top().row,Q.top().col,adj_row,adj_col); /*debug(next_dir); printf("\n");*/ if(nodes[Q.top().node_loc].children[next_dir] != -1){ //子に遷移できる //printf("子に遷移できる\n"); next_node_pos = nodes[Q.top().node_loc].children[next_dir]; }else{ //行き止まり //printf("行き止まり\n"); if(Q.top().node_loc == 0){ //原点の場合<suffix_linkが未定義> next_node_pos = 0; if(min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost+1){ min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost+1; Q.push(Info(adj_row,adj_col,Q.top().sum_cost+1,next_node_pos)); } continue; } tmp = nodes[Q.top().node_loc].suffix_link; while(tmp != 0 && nodes[tmp].children[next_dir] == -1){ tmp = nodes[tmp].suffix_link; } //遷移先、およびそのsiffix_link列の中にfinish_FLGがtrueになっているものがないか調べる tmp = next_node_pos; //printf("next_node_pos:%d\n",next_node_pos); while(tmp != 0){ if(nodes[tmp].finish_FLG){ //printf("nodes[%d]でfinish_FLG\n",tmp); FLG = false; break; } tmp = nodes[tmp].suffix_link; } if(!FLG)continue; //禁止パターンに抵触 if(min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost+1){ min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost+1; Q.push(Info(adj_row,adj_col,Q.top().sum_cost+1,next_node_pos)); } } Q.pop(); } } printf("-1\n"); } int main(){ for(int i = 0; i < 11; i++)POW[i] = pow(4,i); table['U'] = North; table['R'] = East; table['D'] = South; table['L'] = West; while(true){ scanf("%d %d",&H,&W); if(H == 0 && W == 0)break; func(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; vector<node_t> nodes; int nodeCount; AhoCorasick(int maxNodes) { nodes.resize(maxNodes); nodes[0] = node_t(); nodes[0].suffLink = 0; nodes[0].parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur].children[c] == -1) { nodes[nodeCount] = node_t(); nodes[nodeCount].parent = cur; nodes[nodeCount].charFromParent = s[i]; nodes[cur].children[c] = nodeCount++; } cur = nodes[cur].children[c]; } nodes[cur].isLeaf = true; } int suffLink(int nodeIndex) { node_t node = nodes[nodeIndex]; if (node.suffLink == -1) { node.suffLink = (node.parent == 0) ? 0 : transition(suffLink(node.parent), node.charFromParent); } return node.suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t node = nodes[nodeIndex]; if (node.transitions[c] == -1) { node.transitions[c] = (node.children[c] != -1) ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node.transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node].isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho(101); for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; dist[now.y][now.x][now.nodeIndex] = now.step; assert(now.nodeIndex < 101); for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (aho.nodes[nextNode].isLeaf) continue; if (dist[ny][nx][nextNode] < now.step + 1) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const int MAX = 50; static const int PMAX = 11; static const string DT = "URDL"; static const int di[4] = {-1, 0, 1, 0}; static const int dj[4] = {0, 1, 0, -1}; int H, W, np; char G[MAX][MAX]; string P[PMAX]; class State { public: int pos, M[PMAX]; State() { for (int i = 0; i < np; i++) M[i] = -1; } bool shift(char ch) { bool suc = true; for (int i = 0; i < np; i++) { if (P[i][M[i] + 1] == ch) { M[i]++; if (M[i] >= P[i].size() - 1) suc = false; } else { M[i] = -1; if (P[i][M[i] + 1] == ch) { M[i]++; if (M[i] >= P[i].size() - 1) suc = false; } } } return suc; } bool operator<(const State &s) const { if (pos != s.pos) return pos < s.pos; for (int i = 0; i < np; i++) { if (M[i] == s.M[i]) continue; return M[i] < s.M[i]; } return false; } }; int bfs(int si, int sj, int gi, int gj) { queue<State> Q; set<State> V; map<State, int> D; State s; s.pos = si * W + sj; D[s] = 0; V.insert(s); Q.push(s); State u, v; int ni, nj; while (!Q.empty()) { u = Q.front(); Q.pop(); if (u.pos / W == gi && u.pos % W == gj) return D[u]; for (int r = 0; r < 4; r++) { ni = u.pos / W + di[r]; nj = u.pos % W + dj[r]; if (ni < 0 || nj < 0 || ni >= H || nj >= W) continue; if (G[ni][nj] == '#') continue; v = u; v.pos = ni * W + nj; if (!v.shift(DT[r])) continue; if (V.find(v) == V.end()) { V.insert(v); D[v] = D[u] + 1; Q.push(v); } } } return -1; } int main() { int si, sj, gi, gj; while (cin >> H >> W && H) { for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { si = i; sj = j; G[i][j] = '.'; } if (G[i][j] == 'G') { gi = i; gj = j; G[i][j] = '.'; } } cin >> np; for (int i = 0; i < np; i++) cin >> P[i]; cout << bfs(si, sj, gi, gj) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; vector<node_t> nodes; int nodeCount; AhoCorasick(int maxNodes) { nodes.resize(maxNodes); nodes[0] = node_t(); nodes[0].suffLink = 0; nodes[0].parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur].children[c] == -1) { nodes[nodeCount] = node_t(); nodes[nodeCount].parent = cur; nodes[nodeCount].charFromParent = s[i]; nodes[cur].children[c] = nodeCount++; } cur = nodes[cur].children[c]; } nodes[cur].isLeaf = true; } int suffLink(int nodeIndex) { node_t node = nodes[nodeIndex]; if (node.suffLink == -1) { node.suffLink = (node.parent == 0) ? 0 : transition(suffLink(node.parent), node.charFromParent); } return node.suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t node = nodes[nodeIndex]; if (node.transitions[c] == -1) { node.transitions[c] = (node.children[c] != -1) ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node.transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node].isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho(101); for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (aho.nodes[nextNode].isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; Trie dp[50][50]; Trie trie; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } string recent10step = next.substr(0, 10); if (dp[dy][dx].exact_match(recent10step)) continue; dp[dy][dx].insert(recent10step); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int64_t inf64 = 1e18; const double eps = 1e-9; template <typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { os << "["; for (const auto& v : vec) { os << v << ","; } os << "]"; return os; } using i64 = int64_t; const int alphabet_size = 256; class pma { public: pma *next[alphabet_size], *failure; vector<int> ids; pma() { fill(next, next + alphabet_size, (pma*)0); failure = NULL; } void insert(const string& str, const int id) { insert(str, 0, id); } void insert(const string& str, const int idx, const int id) { if (idx == str.size()) { ids.push_back(id); return; } if (next[str[idx]] == NULL) this->next[str[idx]] = new pma(); next[str[idx]]->insert(str, idx + 1, id); } }; class aho_corasick { public: vector<string> dic; pma* root; aho_corasick(const vector<string>& dic_) : dic(dic_), root(new pma()) { for (int i = 0; i < dic.size(); ++i) root->insert(dic[i], i); root->failure = root; queue<pma*> que; que.push(root); while (!que.empty()) { pma* curr = que.front(); que.pop(); for (int i = 0; i < alphabet_size; ++i) { pma* next = curr->next[i]; if (next == NULL) { if (curr == root) curr->next[i] = root; else curr->next[i] = curr->failure->next[i]; continue; } if (curr == root) next->failure = root; else next->failure = curr->failure->next[i]; next->ids.insert(next->ids.end(), curr->ids.begin(), curr->ids.end()); que.push(next); } } } vector<bool> match(const string& s) const { vector<bool> res(dic.size()); const pma* state = root; for (char c : s) { state = state->next[c]; for (int i : state->ids) res[i] = true; } return res; } }; void solve(int N, int M) { vector<string> vs(N); int sy, sx, gy, gx; for (int i = (0); i < (N); ++i) { cin >> vs[i]; for (int j = (0); j < (M); ++j) { if (vs[i][j] == 'S') { sy = i; sx = j; } if (vs[i][j] == 'G') { gy = i; gx = j; } } } int P; cin >> P; vector<string> ban(P); for (int i = (0); i < (P); ++i) { cin >> ban[i]; for (char& c : ban[i]) { if (c == 'R') c = 0; if (c == 'U') c = 1; if (c == 'L') c = 2; if (c == 'D') c = 3; } } aho_corasick ac(ban); vector<int> dx = {1, 0, -1, 0}, dy = {0, -1, 0, 1}; queue<tuple<int, int, pma*, int>> que; set<tuple<int, int, pma*>> visited; que.push(make_tuple(sy, sx, ac.root, 0)); visited.insert(make_tuple(sy, sx, ac.root)); while (!que.empty()) { int y, x, d; pma* curr; tie(y, x, curr, d) = que.front(); que.pop(); if (y == gy and x == gx) { cout << d << endl; return; } for (int i = (0); i < (4); ++i) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 or N <= ny or nx < 0 or M <= nx or vs[ny][nx] == '#') continue; pma* next = curr->next[i]; if (!next->ids.empty()) continue; if (visited.find(make_tuple(ny, nx, next)) != visited.end()) continue; que.push(make_tuple(ny, nx, next, d + 1)); visited.insert(make_tuple(ny, nx, next)); } } cout << -1 << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); for (;;) { int N, M; cin >> N >> M; if (!N and !M) break; solve(N, M); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
UTPC 2010 Stolen Jewel
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class PMA { int id; bool tail; PMA *failure; PMA *next[256]; public: PMA(int id = 0) : id(id), tail(false), failure(0) { memset(next, 0, sizeof(next)); } ~PMA() { for (int i = 0; i < 256; i++) { if (next[i] && next[i] != this) delete next[i]; } } int getID() const { return id; } bool isTail() const { return tail; } void build(const vector<string> &p) { int num = 1; for (int i = 0; i < (int)p.size(); i++) { const string &s = p[i]; PMA *t = this; for (int j = 0; j < (int)s.size(); j++) { if (t->next[s[j]]) t = t->next[s[j]]; else t = t->next[s[j]] = new PMA(num++); } t->tail = true; } queue<PMA *> q; for (int i = 0; i < 256; i++) { if (next[i]) { q.push(next[i]); next[i]->failure = this; } else next[i] = this; } while (!q.empty()) { PMA *t = q.front(); q.pop(); for (int i = 0; i < 256; i++) { if (t->next[i]) { q.push(t->next[i]); PMA *r = t->failure; while (!r->next[i]) r = r->failure; t->next[i]->failure = r->next[i]; } } } } PMA *step(char c) const { const PMA *t = this; while (!t->next[c]) t = t->failure; return t->next[c]; } }; string ch("DRUL"); int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; int h, w; char m[52][52]; int dp[51][51][128]; struct Node { int y, x; PMA *p; Node() {} }; int bfs(PMA &pma) { memset(dp, -1, sizeof(dp)); Node a, b; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) if (m[i][j] == 'S') { a.y = i; a.x = j; a.p = &pma; } queue<Node> q; dp[a.y][a.x][a.p->getID()] = 0; q.push(a); while (!q.empty()) { a = q.front(); q.pop(); int d = dp[a.y][a.x][a.p->getID()]; if (m[a.y][a.x] == 'G') return d; for (int i = 0; i < 4; i++) { b.y = a.y + dy[i]; b.x = a.x + dx[i]; if (m[b.y][b.x] == '#') continue; b.p = a.p->step(ch[i]); if (b.p->isTail()) continue; if (dp[b.y][b.x][b.p->getID()] == -1) { dp[b.y][b.x][b.p->getID()] = d + 1; q.push(b); } } } return -1; } int main() { while (cin >> h >> w, h || w) { memset(m, '#', sizeof(m)); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> m[i][j]; int n; cin >> n; vector<string> p; for (int i = 0; i < n; i++) { string s; cin >> s; p.push_back(s); } PMA pma; pma.build(p); cout << bfs(pma) << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; class State { public: int cost; int route; char x; char y; State(char _x, char _y, int _c, int _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int ten2six(long num) { int res = 0; while (num > 0) { res += num % 6; num /= 6; res *= 6; } return res; } set<int> dp[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; for (int i = 0; i < 50; i++) for (int j = 0; j < 50; j++) dp[i][j].clear(); char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; string num_str = ""; for (int i = 0; i < str.size(); i++) { if (str[i] == 'U') { num_str += "1"; } else if (str[i] == 'R') { num_str += "2"; } else if (str[i] == 'D') { num_str += "3"; } else if (str[i] == 'L') { num_str += "4"; } } reverse(num_str.begin(), num_str.end()); trie.insert(num_str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); long route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); string route_str; stringstream ss; ss << route; ss >> route_str; if (route_str == "0") route_str = ""; for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string front; front = (char)((i + 1) + '0'); string next = front + route_str; next = next.substr(0, 10); if (trie.common_prefix_search(next)) { continue; } int next_num = atol(next.substr(0, 9).c_str()); if (dp[dy][dx].count(ten2six(next_num))) continue; dp[dy][dx].insert(ten2six(next_num)); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next_num)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; Trie dp[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (dp[dy][dx].exact_match(next)) continue; dp[dy][dx].insert(next); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> struct Maybe { T val; bool valid; Maybe() : valid(false) {} Maybe(T &t) : val(t), valid(true) {} T &operator=(const T &rv) { val = rv; valid = true; return val; } operator T() { return valid ? val : T(); } const T &fetch(const T &fallback) const { return valid ? val : fallback; } template <typename Cond> bool update(const T &v, Cond cond) { if (!valid || cond(v, val)) { val = v; valid = true; return true; } return false; } bool update(const T &v) { return update(v, less<T>()); } }; template <int CharNum> struct AhoCorasick { struct Node { bool terminal; int failure; array<Maybe<int>, CharNum> edge; Node() : terminal(false), failure(0) { fill(begin(edge), end(edge), Maybe<int>()); } }; vector<Node> g_; int pos_; AhoCorasick(const vector<string> &dict) { g_.resize(1); for (const auto &s : dict) { int cur = 0; for (const int c : s) { if (!g_[cur].edge[c].valid) { g_.push_back(Node()); g_[cur].edge[c] = (int)g_.size() - 1; } cur = g_[cur].edge[c]; } g_[cur].terminal = true; } queue<int> q; q.push(0); while (!q.empty()) { const int cur = q.front(); q.pop(); for (int ch = 0; ch < CharNum; ++ch) { if (g_[cur].edge[ch].valid) { const int n = g_[cur].edge[ch].val; q.push(n); int f = g_[cur].failure; while (f != 0 && !g_[f].edge[ch].valid) { f = g_[f].failure; } g_[n].failure = g_[f].edge[ch].fetch(0); if (g_[n].failure == n) g_[n].failure = 0; } } } } void reset(int pos = 0) { pos_ = pos; } int advance(int ch) { while (pos_ != 0 && !g_[pos_].edge[ch].valid) { pos_ = g_[pos_].failure; } pos_ = g_[pos_].edge[ch].fetch(0); return pos_; } int size() const { return g_.size(); } bool accepted() const { return g_[pos_].terminal; } }; const int DR[] = {0, -1, 0, 1}; const int DC[] = {1, 0, -1, 0}; bool solve(bool first) { int R, C; if (!(cin >> R >> C)) return false; if (!R && !C) return false; vector<string> field(R + 2); pair<int, int> start, goal; field[0] = field[R + 1] = string(C + 2, '#'); for (int r = 1; r < R + 1; ++r) { cin >> field[r]; field[r] = string("#") + field[r] + "#"; for (int c = 1; c < C + 2; ++c) { if (field[r][c] == 'S') { start = make_pair(r, c); } else if (field[r][c] == 'G') { goal = make_pair(r, c); } } } int P; cin >> P; vector<string> prohibited(P); for (int i = 0; i < P; ++i) { cin >> prohibited[i]; for (char &c : prohibited[i]) { switch (c) { case 'R': c = 0; break; case 'U': c = 1; break; case 'L': c = 2; break; case 'D': c = 3; break; } } } AhoCorasick<4> am(prohibited); vector<vector<vector<Maybe<int>>>> memo( R + 2, vector<vector<Maybe<int>>>(C + 2, vector<Maybe<int>>(am.size()))); memo[start.first][start.second][0] = 0; queue<tuple<int, int, int>> q; q.push(make_tuple(start.first, start.second, 0)); int ans = -1; while (!q.empty()) { int r, c, s; tie(r, c, s) = q.front(); q.pop(); if (r == goal.first && c == goal.second) { ans = memo[r][c][s]; break; } for (int i = 0; i < 4; ++i) { am.reset(s); const int nr = r + DR[i]; const int nc = c + DC[i]; const int ns = am.advance(i); if (field[nr][nc] == '#') continue; if (am.accepted()) continue; if (memo[nr][nc][ns].valid) continue; memo[nr][nc][ns] = memo[r][c][s] + 1; q.push(make_tuple(nr, nc, ns)); } } cout << ans << endl; return true; } int main() { cin.tie(0); ios::sync_with_stdio(0); cout.setf(ios::fixed); cout.precision(10); bool first = true; while (solve(first)) { first = false; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct S { int t; int y, x; string h; }; int main() { for (int N, M; cin >> N >> M, N;) { char g[50][51]; int sy, sx; for (int i = 0; i < N; i++) { cin >> g[i]; for (int j = 0; j < M; j++) { if (g[i][j] == 'S') { sy = i; sx = j; } } } int P; cin >> P; string pt[10]; for (int i = 0; i < P; i++) { cin >> pt[i]; } queue<S> que; que.push({0, sy, sx, ""}); set<string> p[50][50]; while (!que.empty()) { S cs = que.front(); if (g[cs.y][cs.x] == 'G') break; que.pop(); if (!p[cs.y][cs.x].insert(cs.h).second) continue; for (int i = 0; i < 4; i++) { int ny = cs.y + (~i & i - 1); int nx = cs.x + (i ? 2 - i : 0); if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') { string ns = cs.h + "URDL"[i]; for (int j = 0; j < P; j++) { if (ns.size() >= pt[j].size() && ns.compare(ns.size() - pt[j].size(), pt[j].size(), pt[j]) == 0) { goto fail; } } que.push({cs.t + 1, ny, nx, ns.substr(ns.size() == 10)}); } fail:; } } cout << (que.empty() ? -1 : que.front().t) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; int h; string route; int x; int y; State(int _x, int _y, int _c, string _r, int _h) : cost(_c), route(_r), x(_x), y(_y), h(_h) {} bool operator<(const State& s) const { return cost + h < s.cost + s.h; } bool operator>(const State& s) const { return cost + h > s.cost + s.h; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, int>, int> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "", 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 10 ? 10 : next.size()); if (dp.find(pair<string, int>(key, dy * 50 + dx)) != dp.end()) continue; dp[pair<string, int>(key, dy * 50 + dx)] = cost + 1; State s_next(dx, dy, cost + 1, next, abs(gy - dy) + abs(gx - dx)); que.push(s_next); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<vector> #include<algorithm> #include<string> #include<queue> #include<cassert> #define INF (1<<29) using namespace std; const string ds="URDL"; const int dx[4]={0,1,0,-1}; const int dy[4]={-1,0,1,0}; int n,m; vector<string>g; struct AhoCorasick{ static const int max_n =101; static const int alph_size=26; struct node_t{ int parent,suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t(){ fill(children,children+alph_size,-1); fill(transitions,transitions+alph_size,-1); suffLink=-1; isLeaf=false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick(){ nodes[0]=new node_t(); nodes[0]->suffLink=0; nodes[0]->parent=-1; nodeCount=1; fill(memo[0],memo[max_n],-2); } void add(string s){ int cur=0; for(int i=0;i<s.size();i++){ int c=s[i]-'A'; if(nodes[cur]->children[c]==-1){ nodes[nodeCount]=new node_t(); nodes[nodeCount]->parent=cur; nodes[nodeCount]->charFromParent=s[i]; nodes[cur]->children[c]=nodeCount++; } cur=nodes[cur]->children[c]; } nodes[cur]->isLeaf=true; } int suffLink(int nodeIndex){ node_t *node=nodes[nodeIndex]; if(node->suffLink==-1){ node->suffLink=(node->parent==0)?0: transition(suffLink(node->parent),node->charFromParent); } return node->suffLink; } int transition(int nodeIndex,char ch){ int c=ch-'A'; node_t *node=nodes[nodeIndex]; if(node->transitions[c]==-1){ node->transitions[c]=(node->children[c]!=-1)?node->children[c]: (nodeIndex==0?0:transition(suffLink(nodeIndex),ch)); } return node->transitions[c]; } int contain(string s){ int node=0,cnt=0; for(int i=0;i<s.size();i++){ node=transition(node,s[i]); cnt+=nodes[node]->isLeaf; } return cnt; } }; struct state{int step,x,y,nodeIndex;}; int dist[51][51][101]; int bfs(){ int p; cin >> p; AhoCorasick aho; for(int i=0;i<p;i++){ string s; cin >> s; aho.add(s); } fill(*dist[0],*dist[51],INF); queue<state>que; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(g[i][j]=='S'){ dist[i][j][0]=0; que.push((state){0,j,i,0}); break; } } } while(!que.empty()){ state now=que.front(); que.pop(); if(g[now.y][now.x]=='G')return now.step; if(dist[now.y][now.x][now.nodeIndex]<now.step)continue; dist[now.y][now.x][now.nodeIndex]=now.step; for(int i=0;i<4;i++){ int nx=now.x+dx[i],ny=now.y+dy[i]; if(nx<0 || ny<0 || m<=nx || n<=ny)continue; if(g[ny][nx]=='#')continue; int nextNode=aho.transition(now.nodeIndex,ds[i]); if(dist[ny][nx][nextNode]<now.step+1)continue; if(aho.nodes[nextNode]->isLeaf)continue; que.push((state){now.step+1,nx,ny,nextNode}); } } return -1; } int main(void){ while(cin >> n >> m,n|m){ g.resize(n); for(int i=0;i<n;i++)cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int64_t inf64 = 1e18; const double eps = 1e-9; template <typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { os << "["; for (const auto& v : vec) { os << v << ","; } os << "]"; return os; } using i64 = int64_t; const int alphabet_size = 256; class pma { public: pma *next[alphabet_size], *failure; vector<int> ids; string s; pma() { fill(next, next + alphabet_size, (pma*)0); failure = NULL; } void insert(const string& str, const int id) { insert(str, 0, id); } void insert(const string& str, const int idx, const int id) { s = str.substr(0, idx); if (idx == str.size()) { ids.push_back(id); return; } if (next[str[idx]] == NULL) this->next[str[idx]] = new pma(); next[str[idx]]->insert(str, idx + 1, id); } }; class aho_corasick { public: vector<string> dic; pma* root; aho_corasick(const vector<string>& dic_) : dic(dic_), root(new pma()) { for (int i = 0; i < dic.size(); ++i) root->insert(dic[i], i); root->failure = root; queue<pma*> que; que.push(root); set<pma*> done; while (!que.empty()) { pma* curr = que.front(); que.pop(); assert(done.find(curr) == done.end()); done.insert(curr); for (int i = 0; i < alphabet_size; ++i) { pma* next = curr->next[i]; if (next == NULL) { if (curr == root) curr->next[i] = root; else curr->next[i] = curr->failure->next[i]; continue; } if (curr == root) next->failure = root; else next->failure = curr->failure->next[i]->failure; que.push(next); } for (int i = 0; i < alphabet_size; ++i) assert(curr->next[i] != NULL); assert(curr->failure != NULL); } } vector<bool> match(const string& s) const { vector<bool> res(dic.size()); const pma* tr = root; for (char c : s) { tr = tr->next[c]; for (int i : tr->ids) res[i] = true; } return res; } }; void solve(int N, int M) { vector<string> vs(N); int sy, sx, gy, gx; for (int i = (0); i < (N); ++i) { cin >> vs[i]; for (int j = (0); j < (M); ++j) { if (vs[i][j] == 'S') { sy = i; sx = j; } else if (vs[i][j] == 'G') { gy = i; gx = j; } } } int P; cin >> P; vector<string> ban(P); for (int i = (0); i < (P); ++i) { cin >> ban[i]; for (char& c : ban[i]) { if (c == 'R') c = 0; if (c == 'U') c = 1; if (c == 'L') c = 2; if (c == 'D') c = 3; } } aho_corasick ac(ban); map<pma*, int> memo[50][50]; vector<int> dx = {1, 0, -1, 0}, dy = {0, -1, 0, 1}; function<int(int, int, pma*)> rec = [&](int y, int x, pma* state) { if (memo[y][x].find(state) != memo[y][x].end()) return memo[y][x][state]; if (y == gy and x == gx) return memo[y][x][state] = 0; memo[y][x][state] = inf; for (int i = (0); i < (4); ++i) { int ny = y + dy[i], nx = x + dx[i]; if (ny < 0 or N <= ny or nx < 0 or M <= nx or vs[ny][nx] == '#') continue; pma* next_state = state->next[i]; if (!next_state->ids.empty()) continue; memo[y][x][state] = min(memo[y][x][state], rec(ny, nx, next_state) + 1); } return memo[y][x][state]; }; int ans = rec(sy, sx, ac.root); if (ans == inf) cout << -1 << endl; else cout << ans << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); for (;;) { int N, M; cin >> N >> M; if (!N and !M) break; solve(N, M); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static char get(int k){ return k==0?'U':k==1?'R':k==2?'D':'L'; } static int get(char k){ return k=='U'?0:k=='R'?1:k=='D'?2:3; } static class V{ public int ID; public V[] c; public boolean leaf; public V(int ID) { this.ID = ID; leaf = false; c = new V[4]; } public void make(String s, int k){ if(k==s.length()){ leaf = true; return; } int d = get(s.charAt(k)); if(c[d]==null){ v[id] = new V(id); c[d] = v[id]; id++; } c[d].make(s, k+1); } public int trans(int dir){ if(c[dir]!=null){ return !leaf?c[dir].ID:-1; } int x = faillink[ID]; if(x==-1)return 0; return v[x].trans(dir); } } static int id; static V[] v; static int[] faillink; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] move = {{-1,0},{0,1},{1,0},{0,-1}}; while(sc.hasNext()){ int h = sc.nextInt(); int w = sc.nextInt(); if((h|w)==0)break; int si = -1; int sj = -1; char[][] map = new char[h][w]; for(int i=0;i<h;i++){ char[] c = sc.next().toCharArray(); for(int j=0;j<w;j++){ map[i][j] = c[j]; if(c[j]=='S'){ si = i; sj = j; } } } int p = sc.nextInt(); String[] pat = new String[p]; v = new V[150]; id = 0; v[0] = new V(id++); for(int i=0;i<p;i++){ pat[i]=sc.next(); v[0].make(pat[i], 0); } faillink = new int[id]; faillink[0] = -1; List<Integer> l = new ArrayList<Integer>(); l.add(0); while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ for(int k=0;k<4;k++){ if(v[i].c[k]!=null){ next.add(v[i].c[k].ID); int x = faillink[i]; while(x!=-1){ if(v[x].c[k]!=null){ faillink[v[i].c[k].ID] = v[x].c[k].ID; break; } x = faillink[v[x].ID]; } if(x==-1)faillink[v[i].c[k].ID] = 0; } } } l = next; } // for(int i=0;i<id;i++)System.out.println("F["+i+"] = " + faillink[i]); //faillink : OK int [][][] dp = new int[h][w][id]; for(int i=0;i<h;i++)for(int j=0;j<w;j++)for(int k=0;k<id;k++)dp[i][j][k]=Integer.MAX_VALUE; List<int[]> list = new ArrayList<int[]>(); list.add(new int[]{si,sj,0}); dp[si][sj][0] = 0; int ans = Integer.MAX_VALUE; while(!list.isEmpty()){ List<int[]> next = new ArrayList<int[]>(); for(int[] a:list){ int i = a[0]; int j = a[1]; int k = a[2]; if(map[i][j]=='G'){ ans = Math.min(ans, dp[i][j][k]); // break; } for(int m=0;m<4;m++){ int ni = i+move[m][0]; int nj = j+move[m][1]; if(0<=ni&&ni<h&&0<=nj&&nj<w&&map[ni][nj]!='#'){ int x = v[k].trans(m); if(x==-1 || v[x].leaf)continue; if(dp[i][j][k]+1 < dp[ni][nj][x]){ dp[ni][nj][x] = dp[i][j][k]+1; next.add(new int[]{ni,nj,x}); } } } } list = next; } System.out.println(ans==Integer.MAX_VALUE?-1:ans); } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int m, n, nr, ns; string mat[53]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; struct Status { int x, y, s, d; Status() {} Status(int dd, int xx, int yy, int ss) : d(dd), x(xx), y(yy), s(ss) {} void Get(int &dd, int &xx, int &yy, int &ss) const { dd = d; xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return d > ts.d; } }; priority_queue<Status> pq; int Dijkstra() { while (!pq.empty()) pq.pop(); memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.push(Status(0, i, j, 0)); goto L; } } } L: int ans = -1; while (!pq.empty()) { int pre, x, y, s; pq.top().Get(pre, x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; if (mat[xx][yy] == 'G') { ans = pre + 1; return ans; } if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.push(Status(pre + 1, xx, yy, ss)); } } } return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int H, W, K, x[60][60], dist[60][60][200], sx, sy, gx, gy, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; string S[10], dir = "DRUL"; vector<tuple<string, int, int>> vec; vector<tuple<int, int, int>> X[60][60][200]; vector<pair<int, int>> banned[200]; void build_banned() { int cnt1 = 1; vec.push_back(make_tuple("", 0, 0)); for (int i = 0; i < K; i++) { for (int j = 0; j <= S[i].size(); j++) { string F = S[i].substr(0, j); bool OK = true; for (int k = 0; k < vec.size(); k++) { if (get<0>(vec[k]) == F) OK = false; } if (OK == true && j != S[i].size()) { vec.push_back(make_tuple(F, cnt1, 0)); cnt1++; } if (OK == true && j == S[i].size()) { vec.push_back(make_tuple(F, cnt1, 1)); cnt1++; } } } for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < 4; j++) { string W2 = get<0>(vec[i]); W2 += dir[j]; bool OK = true; int to = 0; while (W2.size() >= 1) { for (int k = 0; k < vec.size(); k++) { string W3 = get<0>(vec[k]); if (W3 == W2 && get<2>(vec[k]) == 1) { OK = false; } if (W3 == W2 && get<2>(vec[k]) == 0) { if (get<0>(vec[to]).size() < W2.size()) to = k; } } W2 = W2.substr(1, W2.size() - 1); } if (OK == true) banned[i].push_back(make_pair(to, j)); } } } void add_edges() { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { for (int k = 0; k < vec.size(); k++) { if (get<2>(vec[k]) == 1) continue; for (int l = 0; l < 4; l++) { int cx = i + dx[l], cy = j + dy[l]; if (cx <= 0 || cy <= 0 || cx > H || cy > W || x[cx][cy] == 2) continue; for (int m = 0; m < banned[k].size(); m++) { if (banned[k][m].second == l) X[i][j][k].push_back(make_tuple(cx, cy, banned[k][m].first)); } } } } } } int bfs() { queue<tuple<int, int, int>> Q; for (int i = 0; i < 60; i++) { for (int j = 0; j < 60; j++) { for (int k = 0; k < 200; k++) dist[i][j][k] = 999999999; } } Q.push(make_tuple(sx, sy, 0)); dist[sx][sy][0] = 0; while (!Q.empty()) { int a1 = get<0>(Q.front()), a2 = get<1>(Q.front()), a3 = get<2>(Q.front()); Q.pop(); for (int i = 0; i < X[a1][a2][a3].size(); i++) { int tox = get<0>(X[a1][a2][a3][i]), toy = get<1>(X[a1][a2][a3][i]), toz = get<2>(X[a1][a2][a3][i]); if (dist[tox][toy][toz] == 999999999) { dist[tox][toy][toz] = dist[a1][a2][a3] + 1; Q.push(make_tuple(tox, toy, toz)); } } } int maxn = 999999999; for (int i = 0; i < vec.size(); i++) maxn = min(maxn, dist[gx][gy][i]); return maxn; } int main() { while (true) { vec.clear(); for (int i = 0; i < 3600; i++) x[i / 60][i % 60] = 0; for (int i = 0; i < 60; i++) { for (int j = 0; j < 60; j++) { for (int k = 0; k < 200; k++) X[i][j][k].clear(); } } for (int i = 0; i < 200; i++) banned[i].clear(); cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { char c; cin >> c; if (c == '.') { x[i][j] = 1; } if (c == '#') { x[i][j] = 2; } if (c == 'S') { x[i][j] = 1; sx = i; sy = j; } if (c == 'G') { x[i][j] = 1; gx = i; gy = j; } } } cin >> K; for (int i = 0; i < K; i++) { cin >> S[i]; } build_banned(); add_edges(); int ans = bfs(); if (ans == 999999999) ans = -1; cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; class State{ char x, y; short cost; String path; char[] match; State(char x, char y, short cost, String path, char[] match){ this.x = x; this.y = y; this.cost = cost; this.path = path; this.match = match; } public boolean equals(Object o){ State st = (State)o; if(x != st.x || y != st.y){ return false; } for(int i = 0; i < match.length; i++){ if(match[i] != st.match[i]){ return false; } } return true; } public int hashCode(){ int res = x * 841841 + y * 841; for(int i = 0, p = 1; i < match.length; i++, p *= 10){ res += (int)(match[i]) * p; } return res; } } public class Main{ static int h, w, n; static char sx, sy, gx, gy; static char t[][]; static String[] p; static char[] ds = {'U', 'R', 'D', 'L'}; static int dx[] = {0, 1, 0, -1}; static int dy[] = {-1, 0, 1, 0}; static int bfs(){ LinkedList<State> open = new LinkedList<State>(); HashSet<State> closed = new HashSet<State>(); State st = new State(sx, sy, (short)0, "", new char[n]); open.add(st); closed.add(st); while(!open.isEmpty()){ st = open.poll(); //System.out.println(st.path); if(st.x == gx && st.y == gy){ return st.cost; } for(int i = 0; i < 4; i++){ char nx = (char)(st.x + dx[i]); char ny = (char)(st.y + dy[i]); if(0 > nx || 0 > ny || nx >= w || ny >= h || t[ny][nx] == '#'){ continue; } String npath = st.path + ds[i]; if(npath.length() > 10){ npath = npath.substring(1); } boolean flg = true; char[] nmatch = new char[n]; for(int j = 0; j < n; j++){ nmatch[j] = (char)0; for(int len = Math.min(p[j].length(), npath.length()); len > 0; len--){ boolean matchFlg = true; for(int k = 0; k < len; k++){ if(npath.charAt(npath.length() - len + k) != p[j].charAt(k)){ matchFlg = false; break; } } if(matchFlg){ nmatch[j] = (char)len; break; } } if(nmatch[j] == p[j].length()){ flg = false; break; } } if(!flg){ continue; } State nst = new State(nx, ny, (short)(st.cost + 1), npath, nmatch); if(!closed.contains(nst)){ closed.add(nst); open.add(nst); } } } return -1; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ h = sc.nextInt(); w = sc.nextInt(); t = new char[h][w]; for(int i = 0; i < h; i++){ t[i] = sc.next().toCharArray(); for(int j = 0; j < w; j++){ if(t[i][j] == 'S'){ sx = (char)j; sy = (char)i; } else if(t[i][j] == 'G'){ gx = (char)j; gy = (char)i; } } } n = sc.nextInt(); p = new String[n]; for(int i = 0; i < n; i++){ p[i] = sc.next(); } System.out.println(bfs()); } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int proh; int to[4]; int fail; node() { proh = 0; memset(to, 0, sizeof to); fail = 0; } }; int w, h; char field[52][52]; vector<node> ac; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; void addptn(const char *ptn) { int x = 1, f = 0; for (; *ptn; ++ptn) { int d = 0; switch (*ptn) { case 'L': d = 0; break; case 'U': d = 1; break; case 'R': d = 2; break; case 'D': d = 3; break; } if (ac[x].to[d] > 0) { x = ac[x].to[d]; } else { int y = ac.size(); ac[x].to[d] = y; ac.push_back(node()); int f = ac[x].fail; while (ac[f].to[d] == 0) { f = ac[f].fail; } f = ac[f].to[d]; ac[y].fail = f; x = y; } } ac[x].proh = 1; } int getproh(int p) { if (!ac[p].proh) { ac[p].proh = getproh(ac[p].fail); } return ac[p].proh; } int solve() { memset(field, '#', sizeof field); ac.assign(2, node()); fill(ac[0].to, ac[0].to + 4, 1); ac[0].proh = -1; for (int i = 1; i <= h; ++i) { scanf("%s", field[i] + 1); field[i][w + 1] = '#'; } int n; char ptn[16]; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", ptn); addptn(ptn); } const int yofs = 52; const int pofs = yofs * yofs; int sy, sx; for (int y = 1; y <= h; ++y) for (int x = 1; x <= w; ++x) { if (field[y][x] == 'S') { sy = y; sx = x; } } vector<char> vis(pofs * ac.size()); queue<int> q; q.push(sy * yofs + sx + pofs); vis[q.front()] = 1; q.push(-1); int ret = 1; while (q.size() > 1) { int st = q.front(); q.pop(); if (st == -1) { q.push(-1); ++ret; continue; } int y = st % pofs / yofs; int x = st % yofs; int p = st / pofs; for (int i = 0; i < 4; ++i) { int ny = y + dy[i], nx = x + dx[i]; if (field[ny][nx] == '#') { continue; } int np = p; while (ac[np].to[i] == 0) { np = ac[np].fail; } np = ac[np].to[i]; if (getproh(np) == 1) { continue; } int nst = np * pofs + ny * yofs + nx; if (vis[nst]) { continue; } vis[nst] = 1; if (field[ny][nx] == 'G') { return ret; } q.push(nst); } } return -1; } int main() { while (scanf("%d%d", &h, &w), h) { printf("%d\n", solve()); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static char get(int k){ return k==0?'U':k==1?'R':k==2?'D':'L'; } static int get(char k){ return k=='U'?0:k=='R'?1:k=='D'?2:3; } static class V{ public int ID; public V[] c; public boolean leaf; public V(int ID) { this.ID = ID; leaf = false; c = new V[4]; } public void make(String s, int k){ if(k==s.length()){ leaf = true; return; } int d = get(s.charAt(k)); if(c[d]==null){ v[id] = new V(id); c[d] = v[id]; id++; } c[d].make(s, k+1); } public int trans(int dir){ if(c[dir]!=null){ return !leaf?c[dir].ID:-1; } int x = faillink[ID]; if(x==-1)return 0; return v[x].trans(dir); } } static int id; static V[] v; static int[] faillink; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] move = {{-1,0},{0,1},{1,0},{0,-1}}; while(sc.hasNext()){ int h = sc.nextInt(); int w = sc.nextInt(); if((h|w)==0)break; int si = -1; int sj = -1; char[][] map = new char[h][w]; for(int i=0;i<h;i++){ char[] c = sc.next().toCharArray(); for(int j=0;j<w;j++){ map[i][j] = c[j]; if(c[j]=='S'){ si = i; sj = j; } } } int p = sc.nextInt(); String[] pat = new String[p]; v = new V[150]; id = 0; v[0] = new V(id++); for(int i=0;i<p;i++){ pat[i]=sc.next(); v[0].make(pat[i], 0); } faillink = new int[id]; faillink[0] = -1; List<Integer> l = new ArrayList<Integer>(); l.add(0); while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ for(int k=0;k<4;k++){ if(v[i].c[k]!=null){ next.add(v[i].c[k].ID); int x = faillink[i]; while(x!=-1){ if(v[x].c[k]!=null){ faillink[v[i].c[k].ID] = v[x].c[k].ID; break; } x = faillink[v[x].ID]; } if(x==-1)faillink[v[i].c[k].ID] = 0; } } } l = next; } // for(int i=0;i<id;i++)System.out.println("F["+i+"] = " + faillink[i]); //faillink : OK boolean[][][] dp = new boolean[h][w][id]; List<int[]> list = new ArrayList<int[]>(); list.add(new int[]{si,sj,0}); dp[si][sj][0] = true; int step = 0; int ans = -1; while(!list.isEmpty()&&ans==-1){ List<int[]> next = new ArrayList<int[]>(); for(int[] a:list){ int i = a[0]; int j = a[1]; int k = a[2]; if(map[i][j]=='G'){ ans = step; break; } for(int m=0;m<4;m++){ int ni = i+move[m][0]; int nj = j+move[m][1]; if(0<=ni&&ni<h&&0<=nj&&nj<w&&map[ni][nj]!='#'){ int x = v[k].trans(m); if(x==-1 || v[x].leaf)continue; if(!dp[ni][nj][x]){ dp[ni][nj][x] = true; next.add(new int[]{ni,nj,x}); } } } } list = next; step++; } System.out.println(ans); } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; Trie dp[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (dp[dy][dx].exact_match(next)) continue; dp[dy][dx].insert(next); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, pair<int, int> >, int> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 5 ? 5 : next.size()); if (dp.find(pair<string, pair<int, int> >( key, pair<int, int>(dy, dx))) != dp.end()) continue; dp[pair<string, pair<int, int> >(key, pair<int, int>(dy, dx))] = cost + 1; que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; namespace AhoCorasick { class Node; class SearchMachine; struct MatchingResult { map<string, int> rv; long long id; }; }; // namespace AhoCorasick class AhoCorasick::Node { private: set<string> results; map<char, AhoCorasick::Node*> transitions; vector<AhoCorasick::Node*> v_transitions; char character; AhoCorasick::Node* parent; AhoCorasick::Node* failure; public: Node() : character('\0'), parent(NULL), failure(NULL) {} Node(AhoCorasick::Node* _p, char _c) : parent(_p), character(_c), failure(NULL) {} const char get_char() const { return character; } AhoCorasick::Node* get_parent() const { return parent; } AhoCorasick::Node* get_failure() const { return failure; } void set_failure(AhoCorasick::Node* _n) { failure = _n; } AhoCorasick::Node* get_transition(const char c) { if (transitions.find(c) == transitions.end()) return NULL; return transitions[c]; } const set<string>& get_results() const { return results; } void add_result(const string& str) { results.insert(str); } void add_transition(AhoCorasick::Node* node) { transitions[node->get_char()] = node; v_transitions.push_back(node); } long long get_id() const { return reinterpret_cast<long long>(this); } const vector<AhoCorasick::Node*>& get_transitions() const { return v_transitions; } }; class AhoCorasick::SearchMachine { private: set<string> keywords; AhoCorasick::Node* root; AhoCorasick::Node* state; public: SearchMachine(set<string> _k) : keywords(_k) { _build_tree(); } SearchMachine() { _build_tree(); } void _build_tree() { root = new AhoCorasick::Node(); for (set<string>::iterator it = keywords.begin(); it != keywords.end(); it++) { AhoCorasick::Node* node = root; const string& keyword = *it; for (int i = 0; i < keyword.length(); i++) { AhoCorasick::Node* next_node = node->get_transition(keyword[i]); if (next_node == NULL) { next_node = new AhoCorasick::Node(node, keyword[i]); node->add_transition(next_node); } node = next_node; } node->add_result(keyword); } vector<AhoCorasick::Node*> nodes; for (int i = 0; i < root->get_transitions().size(); i++) { root->get_transitions()[i]->set_failure(root); vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(nodes.size() + root->get_transitions()[i]->get_transitions().size()); merge(nodes.begin(), nodes.end(), root->get_transitions()[i]->get_transitions().begin(), root->get_transitions()[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); nodes.swap(tmp_nodes); } while (nodes.size() > 0) { vector<AhoCorasick::Node*> next_nodes; for (int i = 0; i < nodes.size(); i++) { AhoCorasick::Node* r = nodes[i]->get_parent()->get_failure(); const char c = nodes[i]->get_char(); while ((r != NULL) && (r->get_transition(c) != NULL)) { r = r->get_failure(); } if (r == NULL) { nodes[i]->set_failure(root); } else { AhoCorasick::Node* tc = r->get_transition(c); nodes[i]->set_failure(tc); set<string> results; if (tc != NULL) results = tc->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { nodes[i]->add_result(*it); } } vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(next_nodes.size() + nodes[i]->get_transitions().size()); merge(next_nodes.begin(), next_nodes.end(), nodes[i]->get_transitions().begin(), nodes[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); next_nodes.swap(tmp_nodes); } nodes = next_nodes; } root->set_failure(root); state = root; } MatchingResult feed(const string& text) { MatchingResult mr; int index = 0; while (index < text.length()) { AhoCorasick::Node* trans = NULL; while (state != NULL) { trans = state->get_transition(text[index]); if (state == root || trans != NULL) break; state = state->get_failure(); } if (trans != NULL) { state = trans; } set<string> results; if (state != NULL) results = state->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { mr.rv[*it] = index - it->length() + 1; mr.id = state->get_id(); } index++; } mr.id = state->get_id(); state = root; return mr; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; set<long long> dp[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { dp[i][j].clear(); } } int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); set<string> keywords; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; keywords.insert(str); } AhoCorasick::SearchMachine* sm = new AhoCorasick::SearchMachine(keywords); priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = route + dir_str[i]; AhoCorasick::MatchingResult mr = sm->feed(next); if (dp[dy][dx].count(mr.id)) continue; dp[dy][dx].insert(mr.id); if ((!mr.rv.empty())) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Data { int cur, cost; pair<int, int> info; string debug; Data(int cur = (INT_MAX), int cost = (INT_MAX), pair<int, int> info = pair<int, int>((INT_MAX), (INT_MAX)), string debug = "$$$") : cur(cur), cost(cost), info(info), debug(debug) {} bool operator<(const Data& a) const { return cost > a.cost; } }; int h, w, sp, gp, P; char G[60][60]; pair<int, int> next_state[15][15][4]; int mincost[60 * 60][15][15]; string spell[15]; int dx[] = {+0, +1, +0, -1}; int dy[] = {-1, +0, +1, +0}; char dc[] = {'U', 'R', 'D', 'L'}; pair<int, int> getValue(string s) { for (int i = 0; i < P + 1; i++) { int cur = 0; for (int j = 0; j < spell[i].size(); j++) { if (s[cur] != spell[i][j]) { break; } cur++; if (cur >= s.size()) { return pair<int, int>(i, j + 1); } } } return pair<int, int>(0, 0); } void INIT() { for (int i = 0; i < 15; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 4; k++) next_state[i][j][k] = pair<int, int>(0, 0); for (int i = 0; i < P + 1; i++) { for (int j = 0; j < spell[i].size() + 1; j++) { for (int dir = 0; dir < 4; dir++) { for (int k = 0; k < j + 1; k++) { pair<int, int> value = getValue(spell[i].substr(0, j).substr(k) + dc[dir]); if (value != pair<int, int>(0, 0) && next_state[i][j][dir].second < value.second) { next_state[i][j][dir] = value; } } } } } } inline bool isValid(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); } void compute() { priority_queue<Data> Q; Q.push(Data(sp, 0, pair<int, int>(0, 0))); for (int i = 0; i < h * w; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 15; k++) mincost[i][j][k] = (INT_MAX); mincost[sp][0][0] = 0; while (!Q.empty()) { Data data = Q.top(); Q.pop(); if (data.cur == gp) { cout << data.cost << endl; return; } for (int dir = 0; dir < 4; dir++) { int nx = data.cur % w + dx[dir]; int ny = data.cur / w + dy[dir]; if (!isValid(nx, ny)) continue; if (G[ny][nx] == '#') continue; pair<int, int> next = next_state[data.info.first][data.info.second][dir]; if (next.first != 0 && spell[next.first].size() == next.second) continue; if (mincost[nx + ny * w][next.first][next.second] > data.cost + 1) { mincost[nx + ny * w][next.first][next.second] = data.cost + 1; Q.push(Data(nx + ny * w, data.cost + 1, next, data.debug + dc[dir])); } } } cout << -1 << endl; } int main() { while (cin >> h >> w, h | w) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { sp = j + i * w; } else if (G[i][j] == 'G') { gp = j + i * w; } } } cin >> P; spell[0].clear(); for (int i = 0; i < P; i++) { cin >> spell[i + 1]; } INIT(); compute(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; class State { public: int cost; long route; int x; int y; State(int _x, int _y, int _c, long _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; set<long> dp[50][50]; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; string num_str = ""; for (int i = 0; i < str.size(); i++) { if (str[i] == 'U') { num_str += "1"; } else if (str[i] == 'R') { num_str += "2"; } else if (str[i] == 'D') { num_str += "3"; } else if (str[i] == 'L') { num_str += "4"; } } reverse(num_str.begin(), num_str.end()); trie.insert(num_str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); long route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); string route_str; stringstream ss; ss << route; ss >> route_str; if (route_str == "0") route_str = ""; for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string front; front = (char)((i + 1) + '0'); string next = front + route_str; next = next.substr(0, 10); if (trie.common_prefix_search(next)) { continue; } long next_num = atol(next.c_str()); if (dp[dy][dx].count(next_num)) continue; dp[dy][dx].insert(next_num); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next_num)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct S { int t; int y, x; string h; }; int main() { for (int N, M; cin >> N >> M, N;) { char g[50][51]; int sy, sx; for (int i = 0; i < N; i++) { cin >> g[i]; for (int j = 0; j < M; j++) { if (g[i][j] == 'S') { sy = i; sx = j; } } } int P; cin >> P; string pt[10]; for (int i = 0; i < P; i++) { cin >> pt[i]; } queue<S> que; que.push({0, sy, sx, ""}); set<string> p[50][50]; while (!que.empty()) { S cs = que.front(); if (g[cs.y][cs.x] == 'G') break; que.pop(); for (int i = 0; i < 4; i++) { int ny = cs.y + (~i & i - 1); int nx = cs.x + (i ? 2 - i : 0); if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') { string ns = cs.h + "URDL"[i]; for (int j = 0; j < P; j++) { if (ns.size() >= pt[j].size() && ns.compare(ns.size() - pt[j].size(), pt[j].size(), pt[j]) == 0) { goto fail; } } string nns = ns.substr(ns.size() == 10); if (!p[ny][nx].insert(nns).second) continue; que.push({cs.t + 1, ny, nx, nns}); } fail:; } } cout << (que.empty() ? -1 : que.front().t) << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, int>, int> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 5 ? 5 : next.size()); if (dp.find(pair<string, int>(key, dy * 50 + dx)) != dp.end()) continue; dp[pair<string, int>(key, dy * 50 + dx)] = cost + 1; que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Node { int x, y, state, step; }; const int maxn = 50 + 5, maxp = 10 + 5, maxstate = 105; const char *URDL = "URDL"; set<int> vis[maxn][maxn]; int n, m, p, nxt[maxstate][4]; string grid[maxn], ban[maxp]; bool ng[maxstate]; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int main() { while (cin >> n >> m && n) { int sx, sy, gx, gy; for (int i = 0; i < n; ++i) { cin >> grid[i]; for (int j = 0; j < m; ++j) { if (grid[i][j] == 'S') sx = i, sy = j; if (grid[i][j] == 'G') gx = i, gy = j; } } cin >> p; for (int i = 0; i < p; ++i) cin >> ban[i]; vector<string> pfx; for (int i = 0; i < p; ++i) for (int j = 0; j <= ban[i].length(); ++j) pfx.push_back(ban[i].substr(0, j)); sort(pfx.begin(), pfx.end()); pfx.erase(unique(pfx.begin(), pfx.end()), pfx.end()); int s = pfx.size(); for (int i = 0; i < s; ++i) { ng[i] = false; for (int j = 0; j < p; ++j) { ng[i] |= pfx[i].length() >= ban[j].length() && pfx[i].substr(pfx[i].length() - ban[j].length(), ban[j].length()) == ban[j]; } for (int j = 0; j < 4; ++j) { string to = pfx[i] + URDL[j]; int k; for (;;) { k = lower_bound(pfx.begin(), pfx.end(), to) - pfx.begin(); if (k < s && pfx[k] == to) break; to = to.substr(1); } nxt[i][j] = k; } } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) vis[i][j].clear(); Node start; start.x = sx, start.y = sy, start.step = 0, start.state = 0; vis[sx][sy].insert(0); queue<Node> q; q.push(start); int ans = -1; while (!q.empty()) { Node u = q.front(); q.pop(); if (u.x == gx && u.y == gy) { ans = u.step; break; } for (int i = 0; i < 4; ++i) { int nx = u.x + dx[i], ny = u.y + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; if (grid[nx][ny] == '#') continue; int nxt_state = nxt[u.state][i]; if (ng[nxt_state] || vis[nx][ny].count(nxt_state)) continue; Node v; v.x = nx, v.y = ny, v.step = u.step + 1, v.state = nxt_state; q.push(v); vis[nx][ny].insert(nxt_state); } } cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static char get(int k){ return k==0?'U':k==1?'R':k==2?'D':'L'; } static int get(char k){ return k=='U'?0:k=='R'?1:k=='D'?2:3; } static class V{ public int ID; public V[] c; public boolean leaf; public V(int ID) { this.ID = ID; leaf = false; c = new V[4]; } public void make(String s, int k){ if(k==s.length()){ leaf = true; return; } int d = get(s.charAt(k)); if(c[d]==null){ v[id] = new V(id); c[d] = v[id]; id++; } c[d].make(s, k+1); } public int trans(int dir){ if(c[dir]!=null)return c[dir].ID; int x = faillink[ID]; if(x==-1)return 0; return v[x].trans(dir); } } static int id; static V[] v; static int[] faillink; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] move = {{-1,0},{0,1},{1,0},{0,-1}}; while(sc.hasNext()){ int h = sc.nextInt(); int w = sc.nextInt(); if((h|w)==0)break; int si = -1; int sj = -1; char[][] map = new char[h][w]; for(int i=0;i<h;i++){ char[] c = sc.next().toCharArray(); for(int j=0;j<w;j++){ map[i][j] = c[j]; if(c[j]=='S'){ si = i; sj = j; } } } int p = sc.nextInt(); String[] pat = new String[p]; v = new V[150]; id = 0; v[0] = new V(id++); for(int i=0;i<p;i++){ pat[i]=sc.next(); v[0].make(pat[i], 0); } faillink = new int[id]; faillink[0] = -1; List<Integer> l = new ArrayList<Integer>(); l.add(0); while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ for(int k=0;k<4;k++){ if(v[i].c[k]!=null){ next.add(v[i].c[k].ID); int x = faillink[i]; while(x!=-1){ if(v[x].c[k]!=null){ faillink[v[i].c[k].ID] = v[x].c[k].ID; break; } x = faillink[v[x].ID]; } if(x==-1)faillink[v[i].c[k].ID] = 0; } } } l = next; } // for(int i=0;i<id;i++)System.out.println("F["+i+"] = " + faillink[i]); //faillink : OK boolean[][][] dp = new boolean[h][w][id]; List<int[]> list = new ArrayList<int[]>(); list.add(new int[]{si,sj,0}); dp[si][sj][0] = true; int step = 0; int ans = -1; while(!list.isEmpty()&&ans==-1){ List<int[]> next = new ArrayList<int[]>(); for(int[] a:list){ int i = a[0]; int j = a[1]; int k = a[2]; if(map[i][j]=='G'){ ans = step; break; } for(int m=0;m<4;m++){ int ni = i+move[m][0]; int nj = j+move[m][1]; if(0<=ni&&ni<h&&0<=nj&&nj<w&&map[ni][nj]!='#'){ int x = v[k].trans(m); if(v[x].leaf)continue; if(!dp[ni][nj][x]){ dp[ni][nj][x] = true; next.add(new int[]{ni,nj,x}); } } } } list = next; step++; } System.out.println(ans); } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; char dir_idx[256]; int char2idx(char a) { return dir_idx[a]; } public: Trie() { root = new Nodes(); dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; Trie dp[H][W]; Trie trie; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } string recent10step = next.substr(0, 10); if (dp[dy][dx].exact_match(recent10step)) continue; dp[dy][dx].insert(recent10step); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); dp; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Trie { Trie* child[256]; Trie* fail; vector<int> matched; Trie() { for (int i = 0; i < 256; i++) child[i] = NULL; fail = NULL; } ~Trie() {} }; Trie pool[50000]; int pool_pos; inline Trie* alloc() { return &(pool[pool_pos++] = Trie()); } inline void destruct() { pool_pos = 0; } vector<int> unite(const vector<int>& a, const vector<int>& b) { vector<int> ret; set_union((a).begin(), (a).end(), (b).begin(), (b).end(), back_inserter(ret)); return ret; } Trie* build(vector<string> pattern) { Trie* root = alloc(); Trie* now; root->fail = root; for (int i = 0; i < pattern.size(); i++) { now = root; for (int j = 0; j < pattern[i].size(); j++) { int idx = (int)pattern[i][j]; if (now->child[idx] == NULL) { now->child[idx] = alloc(); } now = now->child[idx]; } now->matched.push_back(i); } queue<Trie*> q; for (int i = 0; i < 256; i++) { if ((root->child[i]) == NULL) root->child[i] = root; else { root->child[i]->fail = root; q.push(root->child[i]); } } while (!q.empty()) { now = q.front(); q.pop(); for (int i = 0; i < 256; i++) { if (now->child[i]) { Trie* next = now->fail; while ((next->child[i]) == NULL) next = next->fail; now->child[i]->fail = next->child[i]; now->child[i]->matched = unite(now->child[i]->matched, next->child[i]->matched); q.push(now->child[i]); } } } return root; } void match(Trie*& now, const string s, vector<int>& ret) { for (int i = 0; i < s.size(); i++) { int idx = s[i]; while ((now->child[idx]) == NULL) now = now->fail; now = now->child[idx]; for (int j = 0; j < (now->matched.size()); j++) { ret[now->matched[j]] = 1; } } } struct state { pair<int, int> pos; Trie* trie; int turn; state(pair<int, int> pos, Trie* trie, int turn) : pos(pos), trie(trie), turn(turn) {} }; int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; string dv[4] = {"D", "L", "U", "R"}; string f[55]; int main() { while (1) { int n, m, p; pair<int, int> S, G; set<Trie*> s[55][55]; scanf("%d %d", &n, &m); if (n == 0 && m == 0) break; for (int i = 0; i < n; i++) cin >> f[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (f[i][j] == 'S') { S = pair<int, int>(j, i); } if (f[i][j] == 'G') { G = pair<int, int>(j, i); } } } scanf("%d", &p); vector<string> pat(p); for (int i = 0; i < p; i++) cin >> pat[i]; destruct(); Trie* root = build(pat); queue<state> q; q.push(state(S, root, 0)); int ans = -1; while (!q.empty()) { state now = q.front(); q.pop(); if (now.pos == G) { ans = now.turn; break; } int x = now.pos.first, y = now.pos.second; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue; if (f[ny][nx] == '#') continue; vector<int> v(p, 0); Trie* t = now.trie; match(t, dv[i], v); int flag = 0; for (int j = 0; j < p; j++) flag |= v[j]; if (flag) continue; if (s[nx][ny].find(t) == s[nx][ny].end()) { s[nx][ny].insert(t); q.push(state(pair<int, int>(nx, ny), t, now.turn + 1)); } } } printf("%d\n", ans); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int max_n = 101; static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick() { nodes[0] = new node_t(); nodes[0]->suffLink = 0; nodes[0]->parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur]->children[c] == -1) { nodes[nodeCount] = new node_t(); nodes[nodeCount]->parent = cur; nodes[nodeCount]->charFromParent = s[i]; nodes[cur]->children[c] = nodeCount++; } cur = nodes[cur]->children[c]; } nodes[cur]->isLeaf = true; } int suffLink(int nodeIndex) { node_t *node = nodes[nodeIndex]; if (node->suffLink == -1) { node->suffLink = (node->parent == 0) ? 0 : transition(suffLink(node->parent), node->charFromParent); } return node->suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t *node = nodes[nodeIndex]; if (node->transitions[c] == -1) { node->transitions[c] = (node->children[c] != -1) ? node->children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node->transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node]->isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho; for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (dist[ny][nx][nextNode] < now.step + 1) continue; if (aho.nodes[nextNode]->isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct State { int x, y, e; }; const int INF = (1 << 28); const int MAXH = 51; const int MAXW = 51; const int MAXP = 11; const string dc[] = {"R", "D", "L", "U"}; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int H, W; char G[MAXH][MAXW]; int P; vector<string> S; int E[105][4]; int cost[MAXH][MAXW][105]; int bfs(int sx, int sy, int gx, int gy, int e) { queue<State> que; State s = {sx, sy, e}; fill(cost[0][0], cost[MAXH][0], INF); cost[s.y][s.x][s.e] = 0; que.push(s); while (!que.empty()) { s = que.front(); que.pop(); for (int i = 0; i < 4; ++i) { int nx = s.x + dx[i]; int ny = s.y + dy[i]; int ne = E[s.e][i]; if (ne == -1) continue; if (nx < 0 || nx >= W) continue; if (ny < 0 || ny >= H) continue; if (G[ny][nx] == '#') continue; if (cost[ny][nx][ne] != INF) continue; cost[ny][nx][ne] = cost[s.y][s.x][s.e] + 1; que.push((State){nx, ny, ne}); if (nx == gx && ny == gy) { return cost[ny][nx][ne]; } } } return INF; } int main() { while (cin >> H >> W && (H | W)) { int sx, sy, gx, gy; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> G[i][j]; if (G[i][j] == 'S') { sx = j; sy = i; } if (G[i][j] == 'G') { gx = j; gy = i; } } } cin >> P; S.resize(P); for (int i = 0; i < P; ++i) { cin >> S[i]; } sort(S.begin(), S.end()); vector<string> v; v.push_back(""); for (int i = 0; i < P; ++i) { for (int j = 1; j < S[i].size(); ++j) { string s = S[i].substr(j); if (binary_search(S.begin(), S.end(), s)) continue; v.push_back(s); } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); fill(E[0], E[105], -1); for (int i = 0; i < v.size(); ++i) { for (int j = 0; j < 4; ++j) { string s = v[i] + dc[j]; bool flag = true; while (s.size()) { if (binary_search(S.begin(), S.end(), s)) { flag = false; break; } s = s.substr(1); } if (!flag) continue; s = v[i] + dc[j]; while (1) { int p = lower_bound(v.begin(), v.end(), s) - v.begin(); if (p < v.size() && v[p] == s) { E[i][j] = p; break; } s = s.substr(1); } } } int ans = bfs(sx, sy, gx, gy, 0); if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct State { int x, y, e; }; const int INF = (1 << 28); const int MAXH = 51; const int MAXW = 51; const int MAXP = 11; const string dc[] = {"R", "D", "L", "U"}; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int H, W; char G[MAXH][MAXW]; int P; vector<string> S; int E[105][4]; int cost[MAXH][MAXW][105]; int bfs(int sx, int sy, int gx, int gy, int e) { queue<State> que; State s = {sx, sy, e}; fill(cost[0][0], cost[MAXH][0], INF); cost[s.y][s.x][s.e] = 0; que.push(s); while (!que.empty()) { s = que.front(); que.pop(); for (int i = 0; i < 4; ++i) { int nx = s.x + dx[i]; int ny = s.y + dy[i]; int ne = E[s.e][i]; if (ne == -1) continue; if (nx < 0 || nx >= W) continue; if (ny < 0 || ny >= H) continue; if (G[ny][nx] == '#') continue; if (cost[ny][nx][ne] != INF) continue; cost[ny][nx][ne] = cost[s.y][s.x][s.e] + 1; que.push((State){nx, ny, ne}); if (nx == gx && ny == gy) { return cost[ny][nx][ne]; } } } return INF; } int main() { while (cin >> H >> W && (H | W)) { int sx, sy, gx, gy; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> G[i][j]; if (G[i][j] == 'S') { sx = j; sy = i; } if (G[i][j] == 'G') { gx = j; gy = i; } } } cin >> P; S.resize(P); for (int i = 0; i < P; ++i) { cin >> S[i]; } sort(S.begin(), S.end()); vector<string> v; v.push_back(""); for (int i = 0; i < P; ++i) { for (int j = 1; j < S[i].size(); ++j) { string s = S[i].substr(j); bool flag = true; for (int k = 0; k < S.size(); ++k) { if (s.find(S[k]) != string::npos) { flag = false; break; } } if (flag) { v.push_back(s); } } } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); fill(E[0], E[105], -1); for (int i = 0; i < v.size(); ++i) { for (int j = 0; j < 4; ++j) { string s = v[i] + dc[j]; bool flag = true; for (int k = 0; k < S.size(); ++k) { if (s.find(S[k]) != string::npos) { flag = false; break; } } if (!flag) continue; s = v[i] + dc[j]; while (1) { int p = lower_bound(v.begin(), v.end(), s) - v.begin(); if (p < v.size() && v[p] == s) { E[i][j] = p; break; } s = s.substr(1); } } } int ans = bfs(sx, sy, gx, gy, 0); if (ans == INF) ans = -1; cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; static char dir_idx[256]; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } ~Nodes() { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; int char2idx(char a) { return dir_idx[a]; } public: Trie() { root = new Nodes(); dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { int c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int route2num(const string& str) { int res = 0; for (int i = 0; i < str.size(); i++) { res += dir_idx[str[i]]; res *= 4; } return res / 4; } int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; set<int> dp[50][50]; Trie trie; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route.substr(0, 9); if (trie.common_prefix_search(next)) { continue; } int route_num = route2num(next); if (dp[dy][dx].count(route_num)) continue; dp[dy][dx].insert(route_num); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum DIR { North, East, South, West, }; struct Node { int num, parent_id, children[4], suffix_link, depth; bool finish_FLG; DIR pattern[10]; }; struct Data { void set(int arg_row, int arg_col) { row = arg_row; col = arg_col; } int row, col; }; struct Info { Info(int arg_row, int arg_col, int arg_sum_cost, int arg_node_loc) { row = arg_row; col = arg_col; sum_cost = arg_sum_cost; node_loc = arg_node_loc; } bool operator<(const struct Info &arg) const { return sum_cost > arg.sum_cost; } int row, col, sum_cost, node_loc; }; int H, W, POW[11]; int diff_row[4] = {-1, 0, 0, 1}, diff_col[4] = {0, -1, 1, 0}; int table[128], min_cost[50][50][200]; DIR dir_table[4] = {North, East, South, West}; char base_map[50][51]; Node nodes[200]; Data start, goal; map<int, int> MAP; bool rangeCheck(int row, int col) { if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1) return true; else { return false; } } void debug(DIR dir) { switch (dir) { case North: printf("U"); break; case East: printf("R"); break; case South: printf("D"); break; case West: printf("L"); break; } } void func() { for (int i = 0; i < 200; i++) { nodes[i].finish_FLG = false; for (int k = 0; k < 4; k++) nodes[i].children[k] = -1; } for (int row = 0; row < H; row++) { scanf("%s", base_map[row]); for (int col = 0; col < W; col++) { if (base_map[row][col] == 'S') { start.set(row, col); } else if (base_map[row][col] == 'G') { goal.set(row, col); } } } int P; scanf("%d", &P); nodes[0].parent_id = -1; nodes[0].num = 0; nodes[0].depth = 0; int node_index = 1, tmp_ch, tmp_loc, parent_id, tmp_index; char buf[11]; MAP.clear(); for (int loop = 0; loop < P; loop++) { scanf("%s", buf); tmp_index = 0; tmp_ch = table[buf[tmp_index]]; tmp_loc = 0; tmp_index = 0; while (true) { parent_id = tmp_loc; if (nodes[tmp_loc].children[tmp_ch] == -1) { nodes[tmp_loc].children[tmp_ch] = node_index++; } tmp_loc = nodes[tmp_loc].children[tmp_ch]; nodes[tmp_loc].parent_id = parent_id; for (int i = 0; i < nodes[parent_id].depth; i++) { nodes[tmp_loc].pattern[i] = nodes[parent_id].pattern[i]; } nodes[tmp_loc].pattern[nodes[parent_id].depth] = dir_table[tmp_ch]; nodes[tmp_loc].depth = nodes[parent_id].depth + 1; tmp_index++; nodes[tmp_loc].num = -1; for (int i = 0; i < nodes[tmp_loc].depth; i++) { nodes[tmp_loc].num += (nodes[tmp_loc].pattern[i] + 1) * POW[i]; } MAP[nodes[tmp_loc].num] = tmp_loc; if (buf[tmp_index] == '\0') { nodes[tmp_loc].finish_FLG = true; break; } tmp_ch = table[buf[tmp_index]]; } } int tmp, start_pos; bool Found; for (int i = 1; i < node_index; i++) { Found = false; start_pos = 1; while (start_pos < nodes[i].depth) { tmp = -1; for (int k = start_pos; k < nodes[i].depth; k++) { tmp += (nodes[i].pattern[k] + 1) * POW[k - start_pos]; } auto at = MAP.find(tmp); if (at != MAP.end()) { Found = true; break; } start_pos++; } if (!Found) { nodes[i].suffix_link = 0; } else { nodes[i].suffix_link = MAP[tmp]; } } for (int row = 0; row < H; row++) { for (int col = 0; col < W; col++) { for (int node_loc = 0; node_loc < node_index; node_loc++) min_cost[row][col][node_loc] = 2000000000; } } min_cost[start.row][start.col][0] = 0; priority_queue<Info> Q; Q.push(Info(start.row, start.col, 0, 0)); int next_node_pos, adj_row, adj_col; DIR next_dir; bool FLG; int calc; while (!Q.empty()) { if (Q.top().row == goal.row && Q.top().col == goal.col) { printf("%d\n", Q.top().sum_cost); return; } else if (Q.top().sum_cost > min_cost[Q.top().row][Q.top().col][Q.top().node_loc]) { Q.pop(); } else { for (int i = 0; i < 4; i++) { adj_row = Q.top().row + diff_row[i]; adj_col = Q.top().col + diff_col[i]; if (rangeCheck(adj_row, adj_col) == false || base_map[adj_row][adj_col] == '#') continue; switch (i) { case 0: next_dir = North; break; case 1: next_dir = West; break; case 2: next_dir = East; break; case 3: next_dir = South; break; } if (nodes[Q.top().node_loc].children[next_dir] != -1) { next_node_pos = nodes[Q.top().node_loc].children[next_dir]; } else { if (Q.top().node_loc == 0) { next_node_pos = 0; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push( Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } continue; } tmp = nodes[Q.top().node_loc].suffix_link; while (tmp != 0 && nodes[tmp].children[next_dir] == -1) { tmp = nodes[tmp].suffix_link; } tmp = next_node_pos; while (tmp != 0) { if (nodes[tmp].finish_FLG) { FLG = false; break; } tmp = nodes[tmp].suffix_link; } if (!FLG) continue; if (min_cost[adj_row][adj_col][next_node_pos] > Q.top().sum_cost + 1) { min_cost[adj_row][adj_col][next_node_pos] = Q.top().sum_cost + 1; Q.push(Info(adj_row, adj_col, Q.top().sum_cost + 1, next_node_pos)); } } } Q.pop(); } } printf("-1\n"); } int main() { for (int i = 0; i < 11; i++) POW[i] = pow(4, i); table['U'] = North; table['R'] = East; table['D'] = South; table['L'] = West; while (true) { scanf("%d %d", &H, &W); if (H == 0 && W == 0) break; func(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; class State { public: int cost; long route; int x; int y; State(int _x, int _y, int _c, long _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; int ten2six(long num) { int res = 0; while (num > 0) { res += num % 6; num /= 6; res *= 6; } return res; } int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; set<int> dp[50][50]; char stage[50][50]; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; string num_str = ""; for (int i = 0; i < str.size(); i++) { if (str[i] == 'U') { num_str += "1"; } else if (str[i] == 'R') { num_str += "2"; } else if (str[i] == 'D') { num_str += "3"; } else if (str[i] == 'L') { num_str += "4"; } } reverse(num_str.begin(), num_str.end()); trie.insert(num_str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); long route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); string route_str; stringstream ss; ss << route; ss >> route_str; if (route_str == "0") route_str = ""; for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string front; front = (char)((i + 1) + '0'); string next = front + route_str; next = next.substr(0, 10); if (trie.common_prefix_search(next)) { continue; } long next_num = atol(next.c_str()); if (dp[dy][dx].count(ten2six(next_num))) continue; dp[dy][dx].insert(ten2six(next_num)); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next_num)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class PMA { int id; bool tail; PMA *failure; PMA *next[256]; public: PMA(int id = 0) : id(id), tail(false), failure(0) { memset(next, 0, sizeof(next)); } ~PMA() { for (int i = 0; i < 256; i++) { if (next[i] && next[i] != this) delete next[i]; } } int getID() const { return id; } bool isTail() const { return tail; } void build(const vector<string> &p) { int num = 1; for (int i = 0; i < (int)p.size(); i++) { const string &s = p[i]; PMA *t = this; for (int j = 0; j < (int)s.size(); j++) { if (t->next[s[j]]) t = t->next[s[j]]; else t = t->next[s[j]] = new PMA(num++); } t->tail = true; } queue<PMA *> q; for (int i = 0; i < 256; i++) { if (next[i]) { q.push(next[i]); next[i]->failure = this; } else next[i] = this; } while (!q.empty()) { PMA *t = q.front(); q.pop(); for (int i = 0; i < 256; i++) { if (t->next[i]) { q.push(t->next[i]); PMA *r = t->failure; while (!r->next[i]) r = r->failure; t->next[i]->failure = r->next[i]; } } } } PMA *step(char c) const { const PMA *t = this; while (!t->next[c]) t = t->failure; return t->next[c]; } }; string ch("DRUL"); int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; int h, w; char m[52][52]; int dp[51][51][102]; struct Node { int y, x; PMA *p; Node() {} Node(int y, int x, PMA *p) : y(y), x(x), p(p) {} }; int bfs(PMA &pma) { memset(dp, -1, sizeof(dp)); Node a, b; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) if (m[i][j] == 'S') { a.y = i; a.x = j; a.p = &pma; } queue<Node> q; dp[a.y][a.x][a.p->getID()] = 0; q.push(a); while (!q.empty()) { a = q.front(); q.pop(); int d = dp[a.y][a.x][a.p->getID()]; for (int i = 0; i < 4; i++) { b.y = a.y + dy[i]; b.x = a.x + dx[i]; if (m[b.y][b.x] == '#') continue; b.p = a.p->step(ch[i]); if (b.p->isTail()) continue; if (m[b.y][b.x] == 'G') return d + 1; if (dp[b.y][b.x][b.p->getID()] == -1) { dp[b.y][b.x][b.p->getID()] = d + 1; q.push(b); } } } return -1; } int main() { while (cin >> h >> w, h || w) { memset(m, '#', sizeof(m)); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> m[i][j]; int n; cin >> n; vector<string> p; for (int i = 0; i < n; i++) { string s; cin >> s; p.push_back(s); } PMA pma; pma.build(p); cout << ' ' << bfs(pma) << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0}; const string temp = "RDLU"; const int mask = (1 << 18) - 1; int H, W, P, sx, sy; string S[50], V[10]; int solve() { queue<tuple<int, int, int, int> > que; int min_cost[50][50][10][10]; memset(min_cost, -1, sizeof(min_cost)); que.emplace(sx, sy, 0, 0); min_cost[sx][sy][0][0] = 0; while (!que.empty()) { int x, y, a, b; tie(x, y, a, b) = que.front(); que.pop(); string pat = V[a].substr(0, b); bool f = true; for (int i = 0; i < P; i++) f &= pat.find(V[i]) == string::npos; if (!f) continue; if (S[y][x] == 'G') return (min_cost[x][y][a][b]); for (int i = 0; i < 4; i++) { int nx = x + vx[i], ny = y + vy[i]; if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue; if (S[ny][nx] == '#') continue; string nextpat = pat + string(1, temp[i]); bool match = false; for (int j = 0; j < nextpat.size(); j++) { string st = nextpat.substr(j); for (int k = 0; k < P; k++) { if (V[k].find(st) == 0) { if (min_cost[nx][ny][k][st.size()] == -1) { que.emplace(nx, ny, k, st.size()); min_cost[nx][ny][k][st.size()] = min_cost[x][y][a][b] + 1; } match = true; break; } } if (match) break; } if (!match && min_cost[nx][ny][0][0] == -1) { que.emplace(nx, ny, 0, 0); min_cost[nx][ny][0][0] = min_cost[x][y][a][b] + 1; } } } return (-1); } int main() { while (cin >> H >> W, H) { for (int i = 0; i < H; i++) { cin >> S[i]; for (int j = 0; j < W; j++) { if (S[i][j] == 'S') sx = j, sy = i; } } cin >> P; for (int i = 0; i < P; i++) { cin >> V[i]; } cout << solve() << endl; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class Sit { public: int x, y; vector<int> points; }; int h, w; int n; char field[51][51]; string prohibits[20]; int sy, sx, gx, gy; set<vector<int> > used[51][51]; const int dy[] = {-1, 0, 1, 0}; const int dx[] = {0, 1, 0, -1}; int bfs() { Sit init; init.x = sx; init.y = sy; for (int i = 0; i < n; i++) init.points.push_back(0); queue<Sit> q[2]; int cur = 0; int nxt = 1; q[cur].push(init); used[init.y][init.x].insert(init.points); int cnt = 0; while (q[cur].size()) { while (q[cur].size()) { Sit s = q[cur].front(); q[cur].pop(); if (s.y == gy && s.x == gx) return cnt; for (int i = 0; i < 4; i++) { int ny = s.y + dy[i]; int nx = s.x + dx[i]; if (ny >= 0 && nx >= 0 && ny < h && nx < w && field[ny][nx] == '.') { Sit nsit; nsit.y = ny; nsit.x = nx; nsit.points = s.points; bool ok = true; for (int j = 0; j < n; j++) { if (prohibits[j][s.points[j]] == i + '0') nsit.points[j]++; else { nsit.points[j] = 0; if (prohibits[j][nsit.points[j]] == i + '0') nsit.points[j]++; } if (nsit.points[j] == (int)prohibits[j].size()) { ok = false; break; } } if (!ok) continue; if (used[ny][nx].find(nsit.points) == used[ny][nx].end()) { q[nxt].push(nsit); used[ny][nx].insert(nsit.points); } } } } cnt++; swap(cur, nxt); } return -1; } int main() { while (cin >> h >> w && (h | w)) { for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) used[i][j].clear(); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> field[i][j]; if (field[i][j] == 'S') { sy = i; sx = j; field[i][j] = '.'; } else if (field[i][j] == 'G') { gy = i; gx = j; field[i][j] = '.'; } } } cin >> n; for (int i = 0; i < n; i++) { cin >> prohibits[i]; for (int j = 0; j < (int)prohibits[i].size(); j++) { if (prohibits[i][j] == 'U') prohibits[i][j] = '0'; else if (prohibits[i][j] == 'R') prohibits[i][j] = '1'; else if (prohibits[i][j] == 'D') prohibits[i][j] = '2'; else if (prohibits[i][j] == 'L') prohibits[i][j] = '3'; } } int res = bfs(); cout << res << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const char dd[] = {'L', 'D', 'R', 'U'}; struct Trie { bool leaf; Trie *node[256]; Trie() { leaf = false; for (int i = 0; i < 256; i++) { node[i] = (Trie *)0; } } void insert(const string &str) { Trie *r = this; for (int i = 0; i < str.length(); i++) { char c = str[i]; if (!r->node[c]) r->node[c] = new Trie; r = r->node[c]; } r->leaf = true; } bool find(const string &str) const { const Trie *r = this; for (int i = 0; i < str.length(); i++) { char c = str[i]; if (!r->node[c]) return false; r = r->node[c]; } return r->leaf; } bool findall(const string &str) const { for (int i = 0; i < str.length(); i++) { const Trie *r = this; for (int j = i; j < str.length(); j++) { char c = str[j]; if (!r->node[c]) { r = this; if (r->node[c]) { r = r->node[c]; } } else { r = r->node[c]; if (r->leaf) return true; } } } return false; } }; struct P { int x, y; int step; int moves; string s; }; int main() { int H, W; while (cin >> H >> W, H | W) { vector<vector<bool> > wall(H, vector<bool>(W, '.')); int ix, iy, ox, oy; for (int y = 0; y < (int)(H); ++y) for (int x = 0; x < (int)(W); ++x) { char c; cin >> c; wall[y][x] = c == '#'; switch (c) { case 'S': ix = x; iy = y; break; case 'G': ox = x; oy = y; break; } } Trie trie; int p; cin >> p; for (int i = 0; i < (int)(p); ++i) { string s; cin >> s; trie.insert(s); } map<tuple<int, int, int>, bool> G; queue<P> que; que.push({ix, iy, 0, 0, ""}); while (!que.empty()) { P p = que.front(); que.pop(); int x = p.x, y = p.y; if (x == ox && y == oy) { cout << p.step << endl; goto NEXT; } for (int i = 0; i < (int)(4); ++i) { int sx = x + dx[i], sy = y + dy[i]; if (0 <= sx && sx < W && 0 <= sy && sy < H && !wall[sy][sx]) { int moves = (p.moves << 3) + i; string s = p.s + dd[i]; tuple<int, int, int> t = make_tuple(sx, sy, moves); if (!trie.findall(s) && !((G).find(t) != (G).end())) { G[t] = true; if (s.length() == 11) { s = s.substr(1, 10); } que.push({sx, sy, p.step + 1, moves, s}); } } } } cout << -1 << endl; NEXT : {} } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
/* * AOJ 2212: Stolen Jewel * ???????????°?????????????????°??°???????????¨?????¨???????????????????????¨???????????¶?????????????????¢?????????????????¨???????????????????????¢????¬????????????¶????±???°????????????????°???\??°??? * ?±???????DP+????????? * ??????????????¢?§???????????????????????????¶???????¢??????????????????¨??¶???i????????????????????????????¬???¬?§???°?????????????????¶???j???d[x][y][i]??¨??????(x,y)??¨?????????i???????°???\??°?????¨Dijkstra?????°??°???????????????????????? */ #include <cstdio> #include <queue> #include <cstring> #include <algorithm> #include <iostream> #include <string> using namespace std; int m, n, nr, ns; string mat[53]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; struct Status { int x, y, s, d; Status() {} Status(int dd, int xx, int yy, int ss) : d(dd), x(xx), y(yy), s(ss) {} void Get(int &dd, int &xx, int &yy, int &ss) const { dd = d; xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return d > ts.d; } }; priority_queue<Status> pq; int Dijkstra() { while (!pq.empty()) pq.pop(); memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.emplace(0, i, j, 0); goto L; } } } L: int ans = -1; while (!pq.empty()) { int pre, x, y, s; pq.top().Get(pre, x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; if (pq.size() > 1000) while(); // cout << "pre: " << x << " " << y << " " << pre << endl; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; // cout <<"s=" << suf[s] << " , ss = " << suf[ss] << endl; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; // if (x == 4 && y == 7) cout << xx << yy << mat[xx][yy] << endl; if (mat[xx][yy] == 'G') { // if (x == 4 && y == 7) cout << xx << yy << endl; ans = pre + 1; return ans; } if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.emplace(pre + 1, xx, yy, ss); } } } return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { //freopen("/Users/yogy/acm-challenge-workbook/db.in", "r", stdin); while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const long long INFL = LLONG_MAX; const long double pi = acos(-1); int N, M; int numP; string P[20]; string T[20]; int dehejedj[4] = {-1, 0, 1, 0}; int dehejedjdfejfwjj[4] = {0, 1, 0, -1}; string URDL = "URDL"; int nxt[105][4]; int bad[105]; int dist[55][55][105]; struct state { int x, y, st; }; int main() { while (cin >> N >> M) { memset(nxt, 0, sizeof(nxt)); ; memset(bad, 0, sizeof(bad)); ; memset(dist, 0, sizeof(dist)); ; if (N == 0 && M == 0) return 0; for (int(i) = 0; (i) < (N); (i)++) cin >> T[i]; cin >> numP; for (int(j) = 0; (j) < (numP); (j)++) cin >> P[j]; vector<string> pref; for (int(j) = 0; (j) < (numP); (j)++) for (int(len) = 0; (len) < (int((P[j]).size()) + 1); (len)++) pref.push_back(P[j].substr(0, len)); pref.push_back(""); sort(pref.begin(), pref.end()); pref.erase(unique(pref.begin(), pref.end()), pref.end()); for (int(i) = 0; (i) < (int((pref).size())); (i)++) { for (int(j) = 0; (j) < (numP); (j)++) { if (int((P[j]).size()) <= int((pref[i]).size()) && pref[i].substr(int((pref[i]).size()) - int((P[j]).size())) == P[j]) { bad[i] = 1; } } for (int(j) = 0; (j) < (4); (j)++) { string prefplus = pref[i] + URDL[j]; while (true) { int k = (int)(lower_bound(pref.begin(), pref.end(), prefplus) - pref.begin()); if (k < int((pref).size()) && pref[k] == prefplus) { nxt[i][j] = k; break; } if (int((prefplus).size()) == 0) break; prefplus = prefplus.substr(1); } } } for (int(i) = 0; (i) < (N); (i)++) for (int(j) = 0; (j) < (M); (j)++) for (int(k) = 0; (k) < (int((pref).size())); (k)++) dist[i][j][k] = INF; queue<state> q; for (int(i) = 0; (i) < (N); (i)++) for (int(j) = 0; (j) < (M); (j)++) if (T[i][j] == 'S') { q.push((state){i, j, 0}); dist[i][j][0] = 0; } int ans = INF; while (!q.empty()) { int i = q.front().x; int j = q.front().y; int st = q.front().st; q.pop(); for (int(diri) = 0; (diri) < (4); (diri)++) { int ii = i + dehejedj[diri]; int jj = j + dehejedjdfejfwjj[diri]; if (ii < 0 || N <= ii || jj < 0 || M <= jj) continue; if (T[ii][jj] == '#') continue; int nxtst = nxt[st][diri]; if (bad[nxtst]) continue; if (dist[ii][jj][nxtst] < INF) continue; dist[ii][jj][nxtst] = dist[i][j][st] + 1; if (T[ii][jj] == 'G') ans = min(ans, dist[ii][jj][nxtst]); q.push((state){ii, jj, nxtst}); } } if (ans == INF) ans = -1; cout << ans << '\n'; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int m, n, nr, ns; string mat[53]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; map<char, int> dir; struct Status { int x, y, s, d; Status() {} Status(int dd, int xx, int yy, int ss) : d(dd), x(xx), y(yy), s(ss) {} void Get(int &dd, int &xx, int &yy, int &ss) const { dd = d; xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return d > ts.d; } }; priority_queue<Status> pq; int Dijkstra() { while (!pq.empty()) pq.pop(); memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.push(Status(0, i, j, 0)); goto L; } } } L: int ans = -1; while (!pq.empty()) { int pre, x, y, s; pq.top().Get(pre, x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; if (mat[xx][yy] == 'G') { ans = pre + 1; return ans; } if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.push(Status(pre + 1, xx, yy, ss)); } } } return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { for (int i = 0; i < 4; ++i) { dir[dc[i]] = i; } while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static char get(int k){ return k==0?'U':k==1?'R':k==2?'D':'L'; } static int get(char k){ return k=='U'?0:k=='R'?1:k=='D'?2:3; } static class V{ public int ID; public V[] c; public boolean leaf; public V(int ID) { this.ID = ID; leaf = false; c = new V[4]; } public void make(String s, int k){ if(k==s.length()){ leaf = true; return; } int d = get(s.charAt(k)); if(c[d]==null){ v[id] = new V(id); c[d] = v[id]; id++; } c[d].make(s, k+1); } public int trans(int dir){ if(c[dir]!=null)return c[dir].ID; int x = faillink[ID]; if(x==-1)return 0; return v[x].trans(dir); } } static int id; static V[] v; static int[] faillink; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] move = {{-1,0},{0,1},{1,0},{0,-1}}; while(sc.hasNext()){ int h = sc.nextInt(); int w = sc.nextInt(); int si = -1; int sj = -1; char[][] map = new char[h][w]; for(int i=0;i<h;i++){ char[] c = sc.next().toCharArray(); for(int j=0;j<w;j++){ map[i][j] = c[j]; if(c[j]=='S'){ si = i; sj = j; } } } int p = sc.nextInt(); String[] pat = new String[p]; v = new V[150]; id = 0; v[0] = new V(id++); for(int i=0;i<p;i++){ pat[i]=sc.next(); v[0].make(pat[i], 0); } faillink = new int[id]; faillink[0] = -1; List<Integer> l = new ArrayList<Integer>(); l.add(0); while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ for(int k=0;k<4;k++){ if(v[i].c[k]!=null){ next.add(v[i].c[k].ID); int x = faillink[i]; while(x!=-1){ if(v[x].c[k]!=null){ faillink[v[i].c[k].ID] = v[x].c[k].ID; break; } x = faillink[v[x].ID]; } if(x==-1)faillink[v[i].c[k].ID] = 0; } } } l = next; } // for(int i=0;i<id;i++)System.out.println("F["+i+"] = " + faillink[i]); //faillink : OK boolean[][][] dp = new boolean[h][w][id]; List<int[]> list = new ArrayList<int[]>(); list.add(new int[]{si,sj,0}); dp[si][sj][0] = true; int step = 0; int ans = -1; while(!list.isEmpty()&&ans==-1){ List<int[]> next = new ArrayList<int[]>(); for(int[] a:list){ int i = a[0]; int j = a[1]; int k = a[2]; if(map[i][j]=='G'){ ans = step; break; } for(int m=0;m<4;m++){ int ni = i+move[m][0]; int nj = j+move[m][1]; if(0<=ni&&ni<h&&0<=nj&&nj<w&&map[ni][nj]!='#'){ int x = v[k].trans(m); if(v[x].leaf)continue; if(!dp[ni][nj][x]){ dp[ni][nj][x] = true; next.add(new int[]{ni,nj,x}); } } } } list = next; step++; } System.out.println(ans); } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; class Nodes { public: class Nodes* children[256]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 256; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; public: Trie() { root = new Nodes(); } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = str[i]; if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } }; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class State { public: int cost; int h; string route; int x; int y; State(int _x, int _y, int _c, string _r, int _h) : cost(_c), route(_r), x(_x), y(_y), h(_h) {} bool operator<(const State& s) const { return cost + h < s.cost + s.h; } bool operator>(const State& s) const { return cost + h > s.cost + s.h; } }; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; char stage[50][50]; map<pair<string, short>, bool> dp; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); Trie trie; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "", 0)); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } string key = next.substr(0, next.size() >= 10 ? 10 : next.size()); if (dp.find(pair<string, short>(key, dy * 50 + dx)) != dp.end()) continue; dp[pair<string, short>(key, dy * 50 + dx)] = true; State s_next(dx, dy, cost + 1, next, abs(gy - dy) + abs(gx - dx)); que.push(s_next); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct NODE { int x, y, a, b, cost; NODE(int A, int B, int C, int D, int E) { x = A, y = B, a = C, b = D, cost = E; } }; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; char dc[] = {'L', 'U', 'R', 'D'}; int main() { int W, H; while (cin >> H >> W) { char field[52][52]; for (int i = 0; i < 52; i++) for (int j = 0; j < 52; j++) field[i][j] = '#'; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> field[i + 1][j + 1]; int sx, sy; for (int i = 0; i < W + 1; i++) for (int j = 0; j < W + 1; j++) if (field[i][j] == 'S') sx = j, sy = i; int p; cin >> p; vector<string> P(10); for (int i = 0; i < p; i++) cin >> P[i]; for (int i = 0; i < p; i++) for (int j = 0; j < p - 1; j++) if (P[j].length() < P[j + 1].length()) swap(P[j], P[j + 1]); bool done[52][52][11][11] = {0}; queue<NODE> Q; Q.push(NODE(sx, sy, 0, 0, 0)); while (Q.size()) { NODE q = Q.front(); Q.pop(); string pt = P[q.a].substr(0, q.b); if (pt.size() && count(P.begin(), P.end(), pt)) continue; if (field[q.y][q.x] == 'G') { cout << q.cost << endl; goto end; } if (done[q.y][q.x][q.a][q.b]) continue; else done[q.y][q.x][q.a][q.b] = true; if (field[q.y][q.x] == '#') continue; for (int d = 0; d < 4; d++) { string st, t = pt + dc[d]; for (int i = 0; i < t.size(); i++) { st = t.substr(i); for (int j = 0; j < p; j++) { if (P[j].find(st) == 0) { Q.push(NODE(q.x + dx[d], q.y + dy[d], j, st.size(), q.cost + 1)); goto ooo; } } } Q.push(NODE(q.x + dx[d], q.y + dy[d], 0, 0, q.cost + 1)); ooo:; } } cout << -1 << endl; end:; } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int max_n = 101; static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; node_t *nodes[max_n]; int nodeCount; AhoCorasick() { nodes[0] = new node_t(); nodes[0]->suffLink = 0; nodes[0]->parent = -1; nodeCount = 1; fill(memo[0], memo[max_n], -2); } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur]->children[c] == -1) { nodes[nodeCount] = new node_t(); nodes[nodeCount]->parent = cur; nodes[nodeCount]->charFromParent = s[i]; nodes[cur]->children[c] = nodeCount++; } cur = nodes[cur]->children[c]; } nodes[cur]->isLeaf = true; } int suffLink(int nodeIndex) { node_t *node = nodes[nodeIndex]; if (node->suffLink == -1) { node->suffLink = (node->parent == 0) ? 0 : transition(suffLink(node->parent), node->charFromParent); } return node->suffLink; } int memo[max_n][alph_size]; int transition(int nodeIndex, char ch) { int c = ch - 'A'; if (-2 < memo[nodeIndex][c]) return memo[nodeIndex][c]; node_t *node = nodes[nodeIndex]; if (node->transitions[c] == -1) { node->transitions[c] = (node->children[c] != -1) ? node->children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return memo[nodeIndex][c] = node->transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node]->isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][101]; int bfs() { int p; cin >> p; AhoCorasick aho; for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (dist[ny][nx][nextNode] < now.step + 1) continue; if (aho.nodes[nextNode]->isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class PMA { int id; bool match; PMA *failure; PMA *next[256]; public: PMA(int id = 0) : id(id), match(false), failure(0) { memset(next, 0, sizeof(next)); } ~PMA() { for (int i = 0; i < 256; i++) { if (next[i] && next[i] != this) delete next[i]; } } int getID() const { return id; } bool matched() const { return match; } void build(const vector<string> &p) { int num = 1; for (int i = 0; i < (int)p.size(); i++) { const string &s = p[i]; PMA *t = this; for (int j = 0; j < (int)s.size(); j++) { if (!t->next[s[j]]) t->next[s[j]] = new PMA(num++); t = t->next[s[j]]; } t->match = true; } queue<PMA *> q; for (int i = 0; i < 256; i++) { if (next[i]) { q.push(next[i]); next[i]->failure = this; } else next[i] = this; } while (!q.empty()) { PMA *t = q.front(); q.pop(); for (int i = 0; i < 256; i++) { if (t->next[i]) { q.push(t->next[i]); PMA *r = t->failure; while (!r->next[i]) r = r->failure; t->next[i]->failure = r = r->next[i]; t->next[i]->match |= r->match; } } } } PMA *step(char c) const { const PMA *t = this; while (!t->next[c]) t = t->failure; return t->next[c]; } }; string ch("DRUL"); int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; int h, w; char m[52][52]; int dp[51][51][102]; struct Node { int y, x; PMA *p; Node() {} }; int bfs(PMA &pma) { memset(dp, -1, sizeof(dp)); Node a, b; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) if (m[i][j] == 'S') { a.y = i; a.x = j; a.p = &pma; } queue<Node> q; dp[a.y][a.x][a.p->getID()] = 0; q.push(a); while (!q.empty()) { a = q.front(); q.pop(); int d = dp[a.y][a.x][a.p->getID()]; for (int i = 0; i < 4; i++) { b.y = a.y + dy[i]; b.x = a.x + dx[i]; if (m[b.y][b.x] == '#') continue; b.p = a.p->step(ch[i]); if (b.p->matched()) continue; if (m[b.y][b.x] == 'G') return d + 1; if (dp[b.y][b.x][b.p->getID()] == -1) { dp[b.y][b.x][b.p->getID()] = d + 1; q.push(b); } } } return -1; } int main() { while (cin >> h >> w && (h || w)) { memset(m, '#', sizeof(m)); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> m[i][j]; int n; cin >> n; vector<string> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; } PMA pma; pma.build(p); cout << ' ' << bfs(pma) << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public class Main{ String INPUT = "./data/judge/201709/A2212.txt"; public static void main(String[] args) throws IOException { new Main().run(); } static final char[] TAG = {'D','U','L','R'}; static final int[] R = {1, -1, 0, 0}; static final int[] C = {0, 0, -1, 1}; static final int INF = 0x3f3f3f3f; static int idx = 0; class State{ int i; int j; Node s; State(int i, int j, Node s){ this.i = i; this.j = j; this.s = s; } @Override public int hashCode() { return i * 31 + j * 17 + s.id * 11; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null ||obj.getClass() != getClass()) return false; State o = (State) obj; return o.i == i && o.j == j && o.s == s; } @Override public String toString() { return i + " " + j + " " + s; } } class Node{ Map<Character, Node> cs = new TreeMap<Character, Node>(); Node[] next = new Node[4]; Node fail; boolean accept; int id; Node() {id = idx++;} @Override public String toString() { return id + " " + accept; } } Node root; void AhoCorasick(char[][] p) { int n = p.length; root = new Node(); for (int i = 0; i < n; ++i) { Node t = root; for (char c : p[i]) { if (!t.cs.containsKey(c)) t.cs.put(c, new Node()); t = t.cs.get(c); } t.accept = true; } // fail Queue<Node> que = new LinkedList<Node>(); que.offer(root); while (!que.isEmpty()) { Node r = que.poll(); for (Map.Entry<Character, Node> entry : r.cs.entrySet()) { char c = entry.getKey(); Node v = entry.getValue(); que.offer(v); Node f = r.fail; while (f != null && !f.cs.containsKey(c)) { f = f.fail; } if (f == null) { v.fail = root; } else { v.fail = f.cs.get(c); } } } // next que.offer(root); while (!que.isEmpty()) { Node t = que.poll(); for (int i = 0; i < 4; ++i) { char c = TAG[i]; Node r = t; while (r != null && !r.cs.containsKey(c)) { r = r.fail; } if (r == null) { t.next[i] = root; } else { t.next[i] = r.cs.get(c); } } for (Node r : t.cs.values()) { que.offer(r); } } } int startR; int startC; int endR, endC; int solve(char[][] f) { int N = f.length; int M = f[0].length; int n = ni(); char[][] p = new char[n][]; for (int i = 0; i < n; ++i) { p[i] = ns().toCharArray(); } AhoCorasick(p); State state = new State(startR, startC, root); Queue<State> queue = new LinkedList<>(); Set<State> vis = new HashSet<>(); queue.offer(state); vis.add(state); int turn = 0; int ans = INF; boolean find = false; outer: while (!queue.isEmpty() && !find) { int size = queue.size(); for (int i = 0; i < size; ++i) { State now = queue.poll(); if (now.i == endR && now.j == endC) { ans = turn; find = true; break outer; } for (int j = 0; j < 4; ++j) { int nx = now.i + R[j]; int ny = now.j + C[j]; if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue; if (f[nx][ny] == '#') continue; Node next = now.s.next[j]; if (next.accept) continue; State ns = new State(nx, ny, next); if (vis.contains(ns)) continue; queue.offer(ns); vis.add(ns); } } turn++; } return ans == INF ? -1 : ans; } void read() { while (true) { int N = ni(); if (N == 0) break; int M = ni(); char[][] f = new char[N][M]; for (int i = 0; i < N; ++i) { char[] cs = ns().toCharArray(); for (int j = 0; j < M; ++j) { f[i][j] = cs[j]; if (cs[j] == 'S') { startR = i; startC = j; } if (cs[j] == 'G') { endR = i; endC = j; } } } idx = 0; out.println(solve(f)); } } FastScanner in; PrintWriter out; void run() throws IOException { boolean oj; try { oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode"); } catch (Exception e) { oj = System.getProperty("ONLINE_JUDGE") != null; } InputStream is = oj ? System.in : new FileInputStream(new File(INPUT)); in = new FastScanner(is); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); read(); out.flush(); if (!oj){ System.out.println("[" + (System.currentTimeMillis() - s) + "ms]"); } } public boolean more(){ return in.hasNext(); } public int ni(){ return in.nextInt(); } public long nl(){ return in.nextLong(); } public double nd(){ return in.nextDouble(); } public String ns(){ return in.nextString(); } public char nc(){ return in.nextChar(); } class FastScanner { BufferedReader br; StringTokenizer st; boolean hasNext; public FastScanner(InputStream is) throws IOException { br = new BufferedReader(new InputStreamReader(is)); hasNext = true; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { hasNext = false; return "##"; } } return st.nextToken(); } String next = null; public boolean hasNext(){ next = nextToken(); return hasNext; } public int nextInt() { if (next == null){ hasNext(); } String more = next; next = null; return Integer.parseInt(more); } public long nextLong() { if (next == null){ hasNext(); } String more = next; next = null; return Long.parseLong(more); } public double nextDouble() { if (next == null){ hasNext(); } String more = next; next = null; return Double.parseDouble(more); } public String nextString(){ if (next == null){ hasNext(); } String more = next; next = null; return more; } public char nextChar(){ if (next == null){ hasNext(); } String more = next; next = null; return more.charAt(0); } } static class ArrayUtils { public static void fill(int[][] f, int value) { for (int i = 0; i < f.length; ++i) { Arrays.fill(f[i], value); } } public static void fill(int[][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } public static void fill(int[][][][] f, int value) { for (int i = 0; i < f.length; ++i) { fill(f[i], value); } } } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Data { int cur, cost; pair<int, int> info; string debug; Data(int cur = (INT_MAX), int cost = (INT_MAX), pair<int, int> info = pair<int, int>((INT_MAX), (INT_MAX)), string debug = "$$$") : cur(cur), cost(cost), info(info), debug(debug) {} bool operator<(const Data& a) const { return cost > a.cost; } }; int h, w, sp, gp, P; char G[60][60]; pair<int, int> next_state[15][15][4]; int mincost[60 * 60][15][15]; string spell[15]; int dx[] = {+0, +1, +0, -1}; int dy[] = {-1, +0, +1, +0}; char dc[] = {'U', 'R', 'D', 'L'}; pair<int, int> getValue(string s) { for (int i = 0; i < P + 1; i++) { int cur = 0; for (int j = 0; j < spell[i].size(); j++) { if (s[cur] != spell[i][j]) { break; } cur++; if (cur >= s.size()) { return pair<int, int>(i, j + 1); } } } return pair<int, int>(0, 0); } void INIT() { for (int i = 0; i < 15; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 4; k++) next_state[i][j][k] = pair<int, int>(0, 0); for (int i = 0; i < P + 1; i++) { for (int j = 0; j < spell[i].size() + 1; j++) { for (int dir = 0; dir < 4; dir++) { for (int k = 0; k < j + 1; k++) { pair<int, int> value = getValue(spell[i].substr(0, j).substr(k) + dc[dir]); if (value != pair<int, int>(0, 0) && next_state[i][j][dir].second < value.second) { next_state[i][j][dir] = value; } } } } } } inline bool isValid(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); } void compute() { priority_queue<Data> Q; Q.push(Data(sp, 0, pair<int, int>(0, 0))); for (int i = 0; i < h * w; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 15; k++) mincost[i][j][k] = (INT_MAX); mincost[sp][0][0] = 0; while (!Q.empty()) { Data data = Q.top(); Q.pop(); if (data.cur == gp) { cout << data.cost << endl; return; } for (int dir = 0; dir < 4; dir++) { int nx = data.cur % w + dx[dir]; int ny = data.cur / w + dy[dir]; if (!isValid(nx, ny)) continue; if (G[ny][nx] == '#') continue; pair<int, int> next = next_state[data.info.first][data.info.second][dir]; if (next.first != 0 && spell[next.first].size() == next.second) continue; if (mincost[nx + ny * w][next.first][next.second] > data.cost + 1) { mincost[nx + ny * w][next.first][next.second] = data.cost + 1; Q.push(Data(nx + ny * w, data.cost + 1, next, data.debug + dc[dir])); } } } cout << -1 << endl; } int main() { while (cin >> h >> w, h | w) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { sp = j + i * w; } else if (G[i][j] == 'G') { gp = j + i * w; } } } cin >> P; spell[0].clear(); vector<string> tmp; for (int i = 0; i < P; i++) { cin >> spell[i + 1]; tmp.push_back(spell[i + 1]); } sort(tmp.begin(), tmp.end()); bool out[tmp.size()]; for (int i = 0; i < tmp.size(); i++) out[i] = false; for (int i = 0; i < tmp.size(); i++) { if (out[i]) continue; for (int j = i + 1; j < tmp.size(); j++) { if (out[j]) continue; bool check = true; for (int k = 0; k < tmp[i].size(); k++) { if (tmp[i][k] != tmp[j][k]) { check = false; break; } } if (check) { out[j] = true; } } } vector<string> tmp2; for (int i = 0; i < tmp.size(); i++) if (!out[i]) { tmp2.push_back(tmp[i]); } P = tmp2.size(); for (int i = 0; i < P; i++) { spell[i + 1] = tmp2[i]; } INIT(); compute(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const string ds = "URDL"; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {-1, 0, 1, 0}; int n, m; vector<string> g; struct AhoCorasick { static const int alph_size = 26; struct node_t { int parent, suffLink; char charFromParent; int children[alph_size]; int transitions[alph_size]; bool isLeaf; node_t() { fill(children, children + alph_size, -1); fill(transitions, transitions + alph_size, -1); suffLink = -1; isLeaf = false; } }; vector<node_t> nodes; int nodeCount; AhoCorasick(int maxNodes) { nodes.resize(maxNodes); nodes[0] = node_t(); nodes[0].suffLink = 0; nodes[0].parent = -1; nodeCount = 1; } void add(string s) { int cur = 0; for (int i = 0; i < s.size(); i++) { int c = s[i] - 'A'; if (nodes[cur].children[c] == -1) { nodes[nodeCount] = node_t(); nodes[nodeCount].parent = cur; nodes[nodeCount].charFromParent = s[i]; nodes[cur].children[c] = nodeCount++; } cur = nodes[cur].children[c]; } nodes[cur].isLeaf = true; } int suffLink(int nodeIndex) { node_t node = nodes[nodeIndex]; if (node.suffLink == -1) { node.suffLink = (node.parent == 0) ? 0 : transition(suffLink(node.parent), node.charFromParent); } return node.suffLink; } int transition(int nodeIndex, char ch) { int c = ch - 'A'; node_t node = nodes[nodeIndex]; if (node.transitions[c] == -1) { node.transitions[c] = (node.children[c] != -1) ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch)); } return node.transitions[c]; } int contain(string s) { int node = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { node = transition(node, s[i]); cnt += nodes[node].isLeaf; } return cnt; } }; struct state { int step, x, y, nodeIndex; }; int dist[51][51][1001]; int bfs() { int p; cin >> p; AhoCorasick aho(1001); for (int i = 0; i < p; i++) { string s; cin >> s; aho.add(s); } fill(*dist[0], *dist[51], (1 << 29)); queue<state> que; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 'S') { dist[i][j][0] = 0; que.push((state){0, j, i, 0}); break; } } } while (!que.empty()) { state now = que.front(); que.pop(); if (g[now.y][now.x] == 'G') return now.step; if (dist[now.y][now.x][now.nodeIndex] < now.step) continue; dist[now.y][now.x][now.nodeIndex] = now.step; assert(now.nodeIndex < 100); for (int i = 0; i < 4; i++) { int nx = now.x + dx[i], ny = now.y + dy[i]; if (nx < 0 || ny < 0 || m <= nx || n <= ny) continue; if (g[ny][nx] == '#') continue; int nextNode = aho.transition(now.nodeIndex, ds[i]); if (aho.nodes[nextNode].isLeaf) continue; que.push((state){now.step + 1, nx, ny, nextNode}); } } return -1; } int main(void) { while (cin >> n >> m, n | m) { g.resize(n); for (int i = 0; i < n; i++) cin >> g[i]; cout << bfs() << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; namespace AhoCorasick { class Node; class SearchMachine; struct MatchingResult { map<string, int> rv; long long id; }; }; // namespace AhoCorasick class AhoCorasick::Node { private: set<string> results; map<char, AhoCorasick::Node*> transitions; vector<AhoCorasick::Node*> v_transitions; char character; AhoCorasick::Node* parent; AhoCorasick::Node* failure; public: Node() : character('\0'), parent(NULL), failure(NULL) {} Node(AhoCorasick::Node* _p, char _c) : parent(_p), character(_c), failure(NULL) {} const char get_char() const { return character; } AhoCorasick::Node* get_parent() const { return parent; } AhoCorasick::Node* get_failure() const { return failure; } void set_failure(AhoCorasick::Node* _n) { failure = _n; } AhoCorasick::Node* get_transition(char c) { if (transitions.find(c) == transitions.end()) return NULL; return transitions[c]; } const set<string>& get_results() const { return results; } void add_result(const string& str) { results.insert(str); } void add_transition(AhoCorasick::Node* node) { transitions[node->get_char()] = node; v_transitions.push_back(node); } long long get_id() const { return reinterpret_cast<long long>(this); } const vector<AhoCorasick::Node*>& get_transitions() const { return v_transitions; } }; class AhoCorasick::SearchMachine { private: set<string> keywords; AhoCorasick::Node* root; AhoCorasick::Node* state; public: SearchMachine(set<string> _k) : keywords(_k) { _build_tree(); } SearchMachine() { _build_tree(); } void _build_tree() { root = new AhoCorasick::Node(); for (set<string>::iterator it = keywords.begin(); it != keywords.end(); it++) { AhoCorasick::Node* node = root; const string& keyword = *it; for (int i = 0; i < keyword.length(); i++) { AhoCorasick::Node* next_node = node->get_transition(keyword[i]); if (next_node == NULL) { next_node = new AhoCorasick::Node(node, keyword[i]); node->add_transition(next_node); } node = next_node; } node->add_result(keyword); } vector<AhoCorasick::Node*> nodes; for (int i = 0; i < root->get_transitions().size(); i++) { root->get_transitions()[i]->set_failure(root); vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(nodes.size() + root->get_transitions()[i]->get_transitions().size()); merge(nodes.begin(), nodes.end(), root->get_transitions()[i]->get_transitions().begin(), root->get_transitions()[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); nodes.swap(tmp_nodes); } while (nodes.size() > 0) { vector<AhoCorasick::Node*> next_nodes; for (int i = 0; i < nodes.size(); i++) { AhoCorasick::Node* r = nodes[i]->get_parent()->get_failure(); char c = nodes[i]->get_char(); while ((r != NULL) && (r->get_transition(c) != NULL)) { r = r->get_failure(); } if (r == NULL) { nodes[i]->set_failure(root); } else { AhoCorasick::Node* tc = r->get_transition(c); nodes[i]->set_failure(tc); set<string> results; if (tc != NULL) results = tc->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { nodes[i]->add_result(*it); } } vector<AhoCorasick::Node*> tmp_nodes; tmp_nodes.reserve(next_nodes.size() + nodes[i]->get_transitions().size()); merge(next_nodes.begin(), next_nodes.end(), nodes[i]->get_transitions().begin(), nodes[i]->get_transitions().end(), back_inserter<vector<AhoCorasick::Node*> >(tmp_nodes)); next_nodes.swap(tmp_nodes); } nodes = next_nodes; } root->set_failure(root); state = root; } MatchingResult feed(const string& text) { MatchingResult mr; int index = 0; while (index < text.length()) { AhoCorasick::Node* trans = NULL; while (1) { trans = state->get_transition(text[index]); if (state == root || trans != NULL) break; state = state->get_failure(); } if (trans != NULL) { state = trans; } set<string> results = state->get_results(); for (set<string>::iterator it = results.begin(); it != results.end(); it++) { mr.rv[*it] = index - it->length() + 1; mr.id = state->get_id(); } index++; } mr.id = state->get_id(); state = root; return mr; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; set<long long> dp[50][50]; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) { dp[i][j].clear(); } } int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); set<string> keywords; for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; keywords.insert(str); } AhoCorasick::SearchMachine* sm = new AhoCorasick::SearchMachine(keywords); priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = route + dir_str[i]; AhoCorasick::MatchingResult mr = sm->feed(next); if (dp[dy][dx].count(mr.id)) continue; dp[dy][dx].insert(mr.id); if ((!mr.rv.empty())) { continue; } if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int m, n, nr, ns; string mat[13]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; map<char, int> dir; struct Status { int x, y, s; Status() {} Status(int xx, int yy, int ss) : x(xx), y(yy), s(ss) {} void Get(int &xx, int &yy, int &ss) const { xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return 1; } }; int Dijkstra() { priority_queue<pair<int, Status>, vector<pair<int, Status> >, greater<pair<int, Status> > > pq; memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.push(make_pair(0, Status(i, j, 0))); break; } } } int ans = -1; while (!pq.empty()) { int pre, x, y, s; pre = pq.top().first; pq.top().second.Get(x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; if (mat[xx][yy] == 'G') { ans = pre + 1; return ans; } if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.push(make_pair(pre + 1, Status(xx, yy, ss))); } } } return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { for (int i = 0; i < 4; ++i) { dir[dc[i]] = i; } while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; const static char dir[4] = {'U', 'R', 'D', 'L'}; const static string dir_str[4] = {"U", "R", "D", "L"}; class Nodes { public: class Nodes* children[4]; bool has_word; Nodes() : has_word(false) { for (int i = 0; i < 4; i++) { children[i] = NULL; } } }; class Trie { private: Nodes* root; char dir_idx[256]; public: Trie() { root = new Nodes(); dir_idx['U'] = 0; dir_idx['R'] = 1; dir_idx['D'] = 2; dir_idx['L'] = 3; } void insert(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] == NULL) { current->children[c] = new Nodes(); } current = current->children[c]; } current->has_word = true; } bool common_prefix_search(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (current->has_word) return true; } return false; } bool exact_match(const string& str) { Nodes* current = root; for (int i = 0; i < str.size(); i++) { char c = char2idx(str[i]); if (current->children[c] != NULL) { current = current->children[c]; } else { return false; } if (i == str.size() - 1 && current->has_word) return true; } return false; } int char2idx(char a) { return dir_idx[a]; } }; class State { public: int cost; string route; int x; int y; State(int _x, int _y, int _c, string _r) : cost(_c), route(_r), x(_x), y(_y) {} bool operator<(const State& s) const { return cost < s.cost; } bool operator>(const State& s) const { return cost > s.cost; } }; char stage[50][50]; Trie dp[50][50]; Trie trie; int main() { int H, W; while (~scanf("%d %d", &H, &W)) { if (H == 0 && W == 0) break; int sx = 0; int sy = 0; int gx = 0; int gy = 0; for (int y = 0; y < H; y++) { char line[51]; scanf("%s", line); for (int x = 0; x < W; x++) { stage[y][x] = line[x]; if (line[x] == 'S') { sx = x; sy = y; } else if (line[x] == 'G') { gx = x; gy = y; } } } int total_prohibited_sequences; scanf("%d", &total_prohibited_sequences); for (int sequence_idx = 0; sequence_idx < total_prohibited_sequences; sequence_idx++) { string str; cin >> str; reverse(str.begin(), str.end()); trie.insert(str); } priority_queue<State, vector<State>, greater<State> > que; que.push(State(sx, sy, 0, "")); int res = 0x3f3f3f3f; while (!que.empty()) { State s = que.top(); string route = s.route; int cost = s.cost; int sx = s.x; int sy = s.y; que.pop(); for (int i = 0; i < 4; i++) { int dx = sx + tx[i]; int dy = sy + ty[i]; if (dx < 0 || dx >= W || dy < 0 || dy >= H) continue; if (stage[dy][dx] == '#') continue; string next = dir_str[i] + route; if (trie.common_prefix_search(next)) { continue; } string recent10step = next.substr(0, 10); if (dp[dy][dx].exact_match(recent10step)) continue; dp[dy][dx].insert(recent10step); if (stage[dy][dx] == 'G') { res = cost + 1; goto found; } que.push(State(dx, dy, cost + 1, next)); } } found:; printf("%d\n", res == 0x3f3f3f3f ? -1 : res); } }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from heapq import heappush, heappop from collections import deque def main(): while True: n, m = map(int, input().split()) if n == 0:break mp = ["#" * (m + 2)] + ["#" + input() + "#" for _ in range(n)] + ["#" * (m + 2)] for y in range(1, n + 1): for x in range(1, m + 1): if mp[y][x] == "S": sx, sy = x, y if mp[y][x] == "G": gx, gy = x, y p = int(input()) bans = [input() for _ in range(p)] dist = [[None] * (m + 2) for _ in range(n + 2)] dist[gy][gx] = 0 vec = ((1, 0), (0, -1), (-1, 0), (0, 1)) que = deque() que.append((0, gx, gy)) while que: score, x, y = que.popleft() for dx, dy in vec: nx, ny = x + dx, y + dy if dist[ny][nx] == None and mp[ny][nx] != "#": dist[ny][nx] = score + 1 que.append((score + 1, nx, ny)) def check(com): for ban in bans: if ban in com:return False return True que = [] heappush(que, (0 + dist[sy][sx], 0, sx, sy, "")) save = set() save.add((sx, sy, "")) while que: total, turn, x, y, com = heappop(que) if (x, y) == (gx, gy): print(turn) break nx, ny = x + 1, y if mp[ny][nx] != "#" and check(com + "R") and (nx, ny, (com + "R")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "R")[-9:])) save.add((nx, ny, (com + "R")[-9:])) nx, ny = x - 1, y if mp[ny][nx] != "#" and check(com + "L") and (nx, ny, (com + "L")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "L")[-9:])) save.add((nx, ny, (com + "L")[-9:])) nx, ny = x, y + 1 if mp[ny][nx] != "#" and check(com + "D") and (nx, ny, (com + "D")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "D")[-9:])) save.add((nx, ny, (com + "D")[-9:])) nx, ny = x, y - 1 if mp[ny][nx] != "#" and check(com + "U") and (nx, ny, (com + "U")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "U")[-9:])) save.add((nx, ny, (com + "U")[-9:])) else: print(-1) main()
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Data { int cur, cost; pair<int, int> info; string debug; Data(int cur = (INT_MAX), int cost = (INT_MAX), pair<int, int> info = pair<int, int>((INT_MAX), (INT_MAX)), string debug = "$$$") : cur(cur), cost(cost), info(info), debug(debug) {} bool operator<(const Data& a) const { return cost > a.cost; } }; int h, w, sp, gp, P; char G[60][60]; pair<int, int> next_state[15][15][4]; int mincost[60 * 60][15][15]; string spell[15]; int dx[] = {+0, +1, +0, -1}; int dy[] = {-1, +0, +1, +0}; char dc[] = {'U', 'R', 'D', 'L'}; pair<int, int> getValue(string s) { for (int i = 0; i < P + 1; i++) { int cur = 0; for (int j = 0; j < spell[i].size(); j++) { if (s[cur] != spell[i][j]) { break; } cur++; if (cur >= s.size()) { return pair<int, int>(i, j + 1); } } } return pair<int, int>(0, 0); } void INIT() { for (int i = 0; i < 15; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 4; k++) next_state[i][j][k] = pair<int, int>(0, 0); for (int i = 0; i < P + 1; i++) { for (int j = 0; j < spell[i].size() + 1; j++) { for (int dir = 0; dir < 4; dir++) { for (int k = j; k >= 0; k--) { pair<int, int> value = getValue(spell[i].substr(0, j).substr(k) + dc[dir]); if (value != pair<int, int>(0, 0) && next_state[i][j][dir].second < value.second) { next_state[i][j][dir] = value; } } } } } } inline bool isValid(int x, int y) { return (0 <= x && x < w && 0 <= y && y < h); } void compute() { priority_queue<Data> Q; Q.push(Data(sp, 0, pair<int, int>(0, 0))); for (int i = 0; i < h * w; i++) for (int j = 0; j < 15; j++) for (int k = 0; k < 15; k++) mincost[i][j][k] = (INT_MAX); mincost[sp][0][0] = 0; while (!Q.empty()) { Data data = Q.top(); Q.pop(); if (data.cur == gp) { cout << data.cost << endl; return; } for (int dir = 0; dir < 4; dir++) { int nx = data.cur % w + dx[dir]; int ny = data.cur / w + dy[dir]; if (!isValid(nx, ny)) continue; if (G[ny][nx] == '#') continue; pair<int, int> next = next_state[data.info.first][data.info.second][dir]; if (next.first != 0 && spell[next.first].size() == next.second) continue; if (mincost[nx + ny * w][next.first][next.second] > data.cost + 1) { mincost[nx + ny * w][next.first][next.second] = data.cost + 1; Q.push(Data(nx + ny * w, data.cost + 1, next, data.debug + dc[dir])); } } } cout << -1 << endl; } int main() { while (cin >> h >> w, h | w) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> G[i][j]; if (G[i][j] == 'S') { sp = j + i * w; } else if (G[i][j] == 'G') { gp = j + i * w; } } } cin >> P; spell[0].clear(); for (int i = 0; i < P; i++) { cin >> spell[i + 1]; } INIT(); compute(); } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class PMA { int id; bool tail; PMA *failure; PMA *next[256]; public: PMA(int id = 0) : id(id), tail(false), failure(0) { memset(next, 0, sizeof(next)); } ~PMA() { for (int i = 0; i < 256; i++) { if (next[i] && next[i] != this) delete next[i]; } } int getID() const { return id; } bool isTail() const { return tail; } void build(const vector<string> &p) { int num = 1; for (int i = 0; i < (int)p.size(); i++) { const string &s = p[i]; PMA *t = this; for (int j = 0; j < (int)s.size(); j++) { if (t->next[s[j]]) t = t->next[s[j]]; else t = t->next[s[j]] = new PMA(num++); } t->tail = true; } queue<PMA *> q; for (int i = 0; i < 256; i++) { if (next[i]) { q.push(next[i]); next[i]->failure = this; } else next[i] = this; } while (!q.empty()) { PMA *t = q.front(); q.pop(); for (int i = 0; i < 256; i++) { if (t->next[i]) { q.push(t->next[i]); PMA *r = t->failure; while (!r->next[i]) r = r->failure; t->next[i]->failure = r->next[i]; } } } } PMA *step(char c) { PMA *t = this; while (!t->next[c]) t = t->failure; return t->next[c]; } }; string ch("DRUL"); int dy[] = {1, 0, -1, 0}; int dx[] = {0, 1, 0, -1}; int h, w; char m[52][52]; int dp[51][51][101]; struct Node { int y, x; PMA *p; Node() {} Node(int y, int x, PMA *p) : y(y), x(x), p(p) {} }; int bfs(PMA &pma) { memset(dp, -1, sizeof(dp)); Node a, b; for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) if (m[i][j] == 'S') { a.y = i; a.x = j; a.p = &pma; } queue<Node> q; dp[a.y][a.x][a.p->getID()] = 0; q.push(a); while (!q.empty()) { a = q.front(); q.pop(); int d = dp[a.y][a.x][a.p->getID()]; for (int i = 0; i < 4; i++) { b.y = a.y + dy[i]; b.x = a.x + dx[i]; if (m[b.y][b.x] == '#') continue; b.p = a.p->step(ch[i]); if (b.p->isTail()) continue; if (m[b.y][b.x] == 'G') return d + 1; if (dp[b.y][b.x][b.p->getID()] == -1) { dp[b.y][b.x][b.p->getID()] = d + 1; q.push(b); } } } return -1; } int main() { while (cin >> h >> w, h || w) { memset(m, '#', sizeof(m)); for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> m[i][j]; int n; cin >> n; vector<string> p; for (int i = 0; i < n; i++) { string s; cin >> s; p.push_back(s); } PMA pma; pma.build(p); cout << bfs(pma) << endl; } return 0; }
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from heapq import heappush, heappop from collections import deque def main(): while True: n, m = map(int, input().split()) if n == 0:break mp = ["#" * (m + 2)] + ["#" + input() + "#" for _ in range(n)] + ["#" * (m + 2)] for y in range(1, n + 1): for x in range(1, m + 1): if mp[y][x] == "S": sx, sy = x, y if mp[y][x] == "G": gx, gy = x, y p = int(input()) bans = [input() for _ in range(p)] dist = [[None] * (m + 2) for _ in range(n + 2)] dist[gy][gx] = 0 vec = ((1, 0), (0, -1), (-1, 0), (0, 1)) que = deque() que.append((0, gx, gy)) while que: score, x, y = que.popleft() for dx, dy in vec: nx, ny = x + dx, y + dy if dist[ny][nx] == None and mp[ny][nx] != "#": dist[ny][nx] = score + 1 que.append((score + 1, nx, ny)) if dist[sy][sx] == None: print(-1) continue def check(com): for ban in bans: if ban in com:return False return True que = [] heappush(que, (0 + dist[sy][sx], 0, sx, sy, "")) save = set() save.add((sx, sy, "")) while que: total, turn, x, y, com = heappop(que) if (x, y) == (gx, gy): print(turn) break nx, ny = x + 1, y if mp[ny][nx] != "#" and check(com + "R") and (nx, ny, (com + "R")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "R")[-9:])) save.add((nx, ny, (com + "R")[-9:])) nx, ny = x - 1, y if mp[ny][nx] != "#" and check(com + "L") and (nx, ny, (com + "L")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "L")[-9:])) save.add((nx, ny, (com + "L")[-9:])) nx, ny = x, y + 1 if mp[ny][nx] != "#" and check(com + "D") and (nx, ny, (com + "D")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "D")[-9:])) save.add((nx, ny, (com + "D")[-9:])) nx, ny = x, y - 1 if mp[ny][nx] != "#" and check(com + "U") and (nx, ny, (com + "U")[-9:]) not in save: heappush(que, (turn + 1 + dist[ny][nx], turn + 1, nx, ny, (com + "U")[-9:])) save.add((nx, ny, (com + "U")[-9:])) else: print(-1) main()
p01329 Stolen Jewel
The jewel, a national treasure of the Kingdom of Pal, was stolen by bandits. As an adventurer, you heard the rumor and headed for the thief's hideout and managed to get the jewels back. However, when I went to return the jewel to the castle of the Kingdom of Pal, the guard of the castle said, "My king has not completely trusted you yet. I doubt that the jewel is genuine, and I got it back in the first place. It is a lie, and I suspect that he is a member of the thief who is aiming for the king's life. " You have been challenged by the guards to see if you really can be trusted. The content of the trial is, "Go to the castle's underground warehouse and place the jewel in the designated place. There is a magic circle that reacts when the jewel is genuine. However, do not cause suspicious behavior. Therefore, do not follow the movement pattern that the guards said from the entrance of the underground warehouse to the designated place. " For example, for the shape and prohibition pattern of the underground warehouse shown in the figure below, the movement pattern as shown in the figure must not be adopted. This is because part of the movement pattern (highlighted in red) is included in the prohibited pattern. (In addition, the second and third movements "↓↓" in this movement pattern are also included in the prohibited pattern) <image> On the other hand, a movement pattern as shown in the figure below is allowed because no part of the movement pattern is included in the prohibited pattern. <image> As input, the shape of the underground warehouse and the prohibition pattern are given. Find the minimum number of moves required to move from the entrance to the magic circle without taking the ban pattern. Notes on Test Cases Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format. When n, m is 0, it indicates the end of input. <!- Input The inputs include the shape of the underground warehouse and the prohibition pattern. The shape of the underground warehouse is represented as follows. First, two integers N and M are given. It means the number of rows and columns of the underground warehouse, respectively. (1 ≤ N, M ≤ 50) Subsequently, N lines of character strings consisting of M characters are given. The characters included and their meanings are as follows. Character | Meaning --- | --- S | The entrance to the underground warehouse. Only one is always included per underground warehouse. G | Magic circle. Only one is always included per underground warehouse. . | Aisle. You can pass (if you don't take the prohibition pattern). | Wall. You can't go through the wall. Next, a prohibition pattern is given. The prohibition pattern is expressed as follows. First, one integer P is given. Means the number of prohibited patterns. (0 ≤ P ≤ 10) Subsequently, a character string meaning a prohibition pattern is given over P lines. The characters included in the prohibited pattern and their meanings are as follows. Character | Meaning --- | --- U | ↑ move. Move R |->. D | ↓ movement. L | <-Move. The length of the prohibited pattern is 1 or more and 10 or less. One prohibition pattern may be a substring of another prohibition pattern. It may also include the same prohibition pattern. Output Output an integer that means the minimum number of moves required. If you can't reach the magic circle, output -1. Examples Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 ######## S......G ######## 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL Output 13 Input 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR Output 60 Input 3 8 S......G 2 U D Output 7 Input 6 10 .......... .S........ .......... .......... ........G. .......... 0 Output 10 Input 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD Output -1 Input 7 6 ...... .####. .####. ...S#. ...##. ...##. .....G 3 LD DD LLL 7 8 S#...... .#.####. .#.#G.#. .#.##.#. .#....#. .######. ........ 8 DDDD DDDU UUUU UUUD RRRR RRRL LLLL LLLR 3 8 S......G 2 U D 6 10 .......... .S........ .......... .......... ........G. .......... 0 6 7 ....... ...#... ...#.S. ...###. .G..... ....... 2 LL DD 0 0 Output 13 60 7 10 -1
{ "input": [ "6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n\nS......G\n\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "3 8\n\nS......G\n\n2\nU\nD", "6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL\n7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR\n3 8\n########\nS......G\n########\n2\nU\nD\n6 10\n..........\n.S........\n..........\n..........\n........G.\n..........\n0\n6 7\n.......\n...#...\n...#.S.\n...###.\n.G.....\n.......\n2\nLL\nDD\n0 0", "7 6\n......\n.####.\n.####.\n...S#.\n...##.\n...##.\n.....G\n3\nLD\nDD\nLLL", "7 8\nS#......\n.#.####.\n.#.#G.#.\n.#.##.#.\n.#....#.\n.######.\n........\n8\nDDDD\nDDDU\nUUUU\nUUUD\nRRRR\nRRRL\nLLLL\nLLLR" ], "output": [ "-1", "13\n60\n7\n10\n-1", "7", "10", "13\n60\n7\n10\n-1", "13", "60" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int m, n, nr, ns; string mat[53]; string rule[13]; int d[53][53][113]; int t[113][4]; string suf[113]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char dc[] = {'U', 'R', 'D', 'L'}; struct Status { int x, y, s, d; Status() {} Status(int dd, int xx, int yy, int ss) : d(dd), x(xx), y(yy), s(ss) {} void Get(int &dd, int &xx, int &yy, int &ss) const { dd = d; xx = x; yy = y; ss = s; } bool operator<(const Status &ts) const { return d > ts.d; } }; priority_queue<Status> pq; int Dijkstra() { while (!pq.empty()) pq.pop(); memset(d, 0x3f, sizeof(d)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (mat[i][j] == 'S') { d[i][j][0] = 0; pq.emplace(0, i, j, 0); goto L; } } } L: while (1) ; int ans = -1; while (!pq.empty()) { int pre, x, y, s; pq.top().Get(pre, x, y, s); pq.pop(); if (pre > d[x][y][s]) continue; for (int k = 0; k < 4; ++k) { int xx, yy, ss; ss = t[s][k]; if (ss == -1) continue; xx = x + dx[k]; yy = y + dy[k]; if (mat[xx][yy] == 'G') { ans = pre + 1; return ans; } if (xx >= 0 && xx < m && yy >= 0 && yy < n && mat[xx][yy] != '#' && pre + 1 < d[xx][yy][ss]) { d[xx][yy][ss] = pre + 1; pq.emplace(pre + 1, xx, yy, ss); } } } return ans; } bool IsSuf(string a, string b) { if (a.length() < b.length()) return false; return a.substr(a.length() - b.length()) == b; } void GaoEdge() { sort(suf, suf + ns); memset(t, 0, sizeof(t)); for (int i = 0; i < ns; ++i) { for (int k = 0; k < 4; ++k) { string ts = suf[i] + dc[k]; for (int j = 0; j < nr; ++j) { if (IsSuf(ts, rule[j])) { t[i][k] = -1; break; } } if (t[i][k] != -1) { for (int j = 0; j < ns; ++j) { if (IsSuf(ts, suf[j]) && suf[j].length() > suf[t[i][k]].length()) { t[i][k] = j; } } } } } } int main() { while (cin >> m >> n && m > 0 && n > 0) { for (int i = 0; i < m; ++i) { cin >> mat[i]; } cin >> nr; ns = 0; suf[ns++] = ""; for (int i = 0; i < nr; ++i) { cin >> rule[i]; for (int j = 1; j < rule[i].length(); ++j) { suf[ns++] = rule[i].substr(0, j); } } GaoEdge(); int ans = Dijkstra(); cout << ans << endl; } return 0; }