分类目录归档:算法

洛谷 牛的旅行 Cow Tours

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<list>
#include<functional>
#include<string>
#define INF 0x7fffffff
using namespace std;
int n;
int posx[200], posy[200];
double arr[200][200];
double sglgdis[200];
inline double distance(int x1, int y1, int x2, int y2) { //最后开根号,函数中未开根号
    return sqrt((double)(x1 - x2)*(x1 - x2) + (double)(y1 - y2)*(y1 - y2));
}
void floyd() {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (arr[i][j] > arr[i][k] + arr[k][j]) {
                    arr[i][j] = arr[i][k] + arr[k][j];
                }
            }
        }
    }
}

int main() {
    for (int i = 0; i <= 199; i++) {
        for (int j = 0; j <= 199; j++) {
            arr[i][j] = INF;
        }
    }
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &posx[i], &posy[i]);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int t;
            scanf("%1d", &t);
            arr[i][j] = (t||i==j) ? distance(posx[i], posy[i], posx[j], posy[j]) : INF;
            if (i == j)arr[i][j] = 0;
        }
    }
    floyd();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j] != INF&&arr[i][j] > sglgdis[i]) {
                sglgdis[i] = arr[i][j];
            }
        }
    }
    double mmm = INF;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (arr[i][j] < INF)continue;
            double t = sglgdis[i] + sglgdis[j] + distance(posx[i], posy[i], posx[j], posy[j]);
            if (t < mmm)mmm = t;
        }
    }
    for (int i = 1; i <= n; i++) {
        if (sglgdis[i] > mmm)mmm = sglgdis[i];
    }
    printf("%.6lf\n", mmm);
}

洛谷 穿越栅栏 Overfencing

题号:1519

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<list>
#include<functional>
#include<string>
using namespace std;
int w, h, m=0;
char graph[300][300];
bool visited[300][300];
int steps[300][300];
int check_x[5] = {0,1,-1,0,0},
check_y[5] = {0,0,0,1,-1},
step_x[5] = {0,2,-2,0,0},
step_y[5] = {0,0,0,2,-2};
struct node {
    int x, y, step;
    node(int x1,int y1,int step1) {
        x = x1; y = y1; step = step1;
    }
};
bool check(int cx, int cy, int sx, int sy) {
    if (!visited[sx][sy] && graph[cx][cy] == ' '&&sx % 2 == 0 && sy % 2 == 0 && sx < 2 * h + 1 && sx>1 && sy < 2 * w + 1 && sy>1)return true;
    return false;
}
void BFS(int x,int y) {
    int maxstep = 0;
    queue<node> q;
    memset(visited, false, sizeof(visited));
    q.push(node(x, y, 0));
    while (!q.empty()) {
        node t = q.front(); q.pop();
        if (steps[t.x][t.y]> t.step)steps[t.x][t.y] = t.step;
        if (visited[t.x][t.y])continue;
        visited[t.x][t.y] = true;
        for (int i = 1; i <= 4; i++) {
            if (check(t.x + check_x[i], t.y + check_y[i], t.x + step_x[i], t.y + step_y[i]))q.push(node(t.x + step_x[i], t.y + step_y[i], t.step + 1));
        }
    }
}
int main() {
    cin >> w >> h;
    cin.getline(graph[0] + 1, 280);
    memset(steps, 0x7f, sizeof(steps));
    for (int i = 1; i <= 2 * h + 1; i++) {
        cin.getline(graph[i] + 1, 280);
    }
    for (int i = 2; i <= 2 * w; i++) {
        if (graph[1][i] == ' ')BFS(2, i);
        if (graph[2 * h + 1][i] == ' ')BFS(2 * h, i);
    }
    for (int i = 2; i <= 2 * h; i++) {
        if (graph[i][1] == ' ')BFS(i, 2);
        if (graph[i][2 * w + 1] == ' ')BFS(i, 2 * w);
    }
    for (int i = 2; i <= 2 * h; i+=2) {
        for (int j = 2; j <= 2 * w; j += 2) {
            if (steps[i][j] > m&&steps[i][j]!=0x7f7f7f7f)m = steps[i][j];
        }
    }
    cout << m+1 << endl;
}

洛谷 奶牛家谱 Cow Pedigrees

毫不夸张的说:USACO里2.3节最难的动规、、、
基本是照着题解一个字一个字对的。。。。
题号:1472

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#define MOD 9901
using namespace std;
int n, k;
long long f[210][110],dp[210][110]; //必须是long long
int main(){
    cin >> n >> k;
    //if (n % 2 == 0){
    //  cout << 0 << endl;
    //  return 0;
    //}
    dp[1][1] = 1; f[0][0] = 1;
    for (int i = 1; i <= k; i++)f[1][i] = f[0][i] = 1;
    for (int i = 3; i <= n; i += 2){ //i:Node Num;j:depth
        int j = 1; while ((1 << j) - 1 < i)j++;
        for (; j <= k; j++){
            for (int l = 1; l <= i - 1; l += 2){
                dp[i][j] += dp[l][j - 1] * f[i - l - 1][j - 2];
                dp[i][j] += f[l][j - 2] * dp[i - l - 1][j - 1];
                dp[i][j] += dp[l][j - 1] * dp[i - l - 1][j - 1];
            }
            dp[i][j] %= MOD;
        }
        for (int j = 1; j <= k; j++)f[i][j] = (f[i][j - 1] + dp[i][j]) % MOD;
    }
    cout << dp[n][k] << endl;
}

USACO Section 2.3 Controlling Companies

STDIN/STDOUT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<utility>
#include<algorithm>
#include<map>
#include<vector>
#include<list>
#include<functional>
#include<string>
using namespace std;
int u[10005], v[10005], w[10005], first[105], nxt[10005];
int n, m;
bool control[105],visited[105];
int percentage[105];
void dfs(int node) {
    if (visited[node])return;
    visited[node] = true;
    for (int e = first[node]; e != -1; e = nxt[e]) {
        percentage[v[e]] += w[e];
        if (percentage[v[e]] > 50) {
            control[v[e]] = true;
            dfs(v[e]);
        }
    }
}
int main() {
    memset(first, -1, sizeof(first));
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> u[i] >> v[i] >> w[i];
        nxt[i] = first[u[i]];
        first[u[i]] = i;
        m = max(m, max(u[i], v[i]));
    }
    for (int i = 1; i <= m; i++) {
        memset(visited, false, sizeof(visited));
        memset(control, false, sizeof(control));
        memset(percentage, 0, sizeof(percentage));
        dfs(i);
        for (int j = 1; j <= m; j++) {
            if (i != j&&control[j])cout << i << " " << j << endl;
        }
    }
}

洛谷 序言页码 Preface Numbering

也是一道恶心的题,相比上面那一道一点不会2333.
题号:1465

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
char str[31][5] = {
    " ", "I", "II", "III", "IV", "V", "VI", "VII", "VIII",
    "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX",
    "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC",
    "CM", "M", "MM", "MMM"
};
char arr1[8] = "IVXLCDM";
int num[31] = {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
    300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000
};
int a[26];
void _find(int x){
    int j = 30;
    while (num[j] > x)j--;
    for (; j >= 1; j--){
        if (x >= num[j]){
            x -= num[j];
            for (int i = 0; i < strlen(str[j]); i++){
                a[str[j][i] - 'A']++;
            }
        }
        if (x == 0)return;
    }
}
int main(){
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        _find(i);
    }
    if (a[int('I') - 65] != 0) printf("I %d\n", a[int('I') - 65]);
    if (a[int('V') - 65] != 0) printf("V %d\n", a[int('V') - 65]);
    if (a[int('X') - 65] != 0) printf("X %d\n", a[int('X') - 65]);
    if (a[int('L') - 65] != 0) printf("L %d\n", a[int('L') - 65]);
    if (a[int('C') - 65] != 0) printf("C %d\n", a[int('C') - 65]);
    if (a[int('D') - 65] != 0) printf("D %d\n", a[int('D') - 65]);
    if (a[int('M') - 65] != 0) printf("M %d\n", a[int('M') - 65]);
}

只有这样才能过……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
char str[31][5] = {
    " ", "I", "II", "III", "IV", "V", "VI", "VII", "VIII",
    "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX",
    "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC",
    "CM", "M", "MM", "MMM"
};
char arr[8] = "IVXLCDM";
int num[31] = {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
    300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000
};
int a[26];
void _find(int x){
    int j = 30;
    while (num[j] > x)j--;
    for (; j >= 1; j--){
        if (x >= num[j]){
            x -= num[j];
            for (int i = 0; i < strlen(str[j]); i++){
                a[str[j][i] - 'A']++;
            }
        }
        if (x == 0)return;
    }
}
int main(){
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        _find(i);
    }
    for (int i = 0; i < 8; i++){
        if (a[arr[i] - 'A'] != 0)cout << (char)(arr[i]) << " " << a[arr[i] - 'A'] << endl;
    }
}

这样莫名其妙多出来一行输出@_500,根本不知道怎么回事……

洛谷 派对灯 Party Lamps

USACO原题,比较恶心……看得题解
表示DFS只有三十分,就不用尝试DFS了,去看洛谷题解吧
题号:1468
https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P1468
作者: ☜闪耀星空☞ 更新时间: 2017-08-10 10:11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int n, c, a, t;
bool light[10], dark[10],flag;
int map[10][10] = {
    { 0, 1, 1, 1, 1, 1, 1 }, //0
    { 0, 0, 0, 0, 0, 0, 0 }, //1
    { 0, 0, 1, 0, 1, 0, 1 }, //2
    { 0, 1, 0, 1, 0, 1, 0 }, //3
    { 0, 0, 1, 1, 0, 1, 1 }, //4
    { 0, 1, 0, 0, 1, 0, 0 }, //1,4
    { 0, 1, 1, 0, 0, 0, 1 }, //2,4
    { 0, 0, 0, 1, 1, 1, 0 }, //3,4
};
void print(int x){
    for (int i = 1; i <= 6; i++){
        if (light[i] && !map[x][i] || dark[i] && map[x][i])return;
    }
    flag = true;
    for (int i = 0; i < n; i++)cout << map[x][i % 6 + 1];
    cout << endl;
}
int main(){
    cin >> n >> c;
    while (cin >> t, t != -1)light[(t - 1) % 6 + 1] = true;
    while (cin >> t, t != -1)dark[(t - 1) % 6 + 1] = true;
    c = min(3, c);
    switch (c){
    case 0:print(0); break;
    case 1:print(1); print(2); print(4); print(3); break;
    case 2:print(1); print(7); print(2); print(4); print(3); print(6); print(0); break;
    case 3:print(1); print(7); print(2); print(4); print(5); print(3); print(6); print(0); break;
    }
    if (!flag)cout << "IMPOSSIBLE" << endl;
}

USACO Section 2.1 PROB The Castle

The Castle
IOI’94 – Day 1
In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

Your task is to help Farmer John know the exact room count and sizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their “outer edges” to keep out the wind and rain.

Consider this annotated floorplan of a castle:

     1   2   3   4   5   6   7
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#   
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #   
   #---#########---#####---#---#
 4 # ->#   |   |   |   |   #   #   
   ############################# 

#  = Wall     -,|  = No wall
-> = Points to the wall to remove to
     make the largest possible new room

By way of example, this castle sits on a 7 x 4 base. A “room” includes any set of connected “squares” in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

The castle always has at least two rooms and always has a wall that can be removed.

PROGRAM NAME: castle

INPUT FORMAT

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist and is the sum of up to four integers:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1: Two space-separated integers: M and N
Line 2..: M x N integers, several per line.

SAMPLE INPUT (file castle.in)

7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

OUTPUT FORMAT

The output contains several lines:

Line 1: The number of rooms the castle has.
Line 2: The size of the largest room
Line 3: The size of the largest room creatable by removing one wall
Line 4: The single wall to remove to make the largest room possible

Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose ‘N’ before ‘E’. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

SAMPLE OUTPUT (file castle.out)

5
9
16
4 1 E

题解:无USACO输入输出,使用标准输入输出,题目可参见洛谷P1457

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<utility>
using namespace std;
int m, n;
int arr[55][55];
bool visited[55][55];
pair<int,int> father[55][55];
int mapsize[55][55];
inline int _find(int x, int y) {
    return mapsize[father[x][y].first][father[x][y].second];
}
inline int getBit(int n, int pos) {
    return (n >> pos) & 1;
}
int dfs(int x, int y,int fatherx,int fathery) {
    if (x<1||x>n||y<1||y>m||visited[x][y])return 0;
    visited[x][y] = true;
    father[x][y] = pair<int, int>(fatherx, fathery);
    int tot = 0;
    if (!getBit(arr[x][y], 0))tot += dfs(x , y-1, fatherx, fathery);
    if (!getBit(arr[x][y], 1))tot += dfs(x-1, y, fatherx, fathery);
    if (!getBit(arr[x][y], 2))tot += dfs(x, y+1, fatherx, fathery);
    if (!getBit(arr[x][y], 3))tot += dfs(x+1, y, fatherx, fathery);
    return tot+1;
}
int main() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            father[i][j] = pair<int, int>(-1, -1);
        }
    }
    cin >> m >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> arr[i][j];
        }
    }
    int maxs = 0,room=0;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int t = dfs(i, j, i, j);
            if (t != 0) {
                room++;
                mapsize[i][j] = t;
            }
            if (t > maxs)maxs = t;
        }
    }
    cout << room << endl << maxs << endl;
    int maxcombsquare = 0, wallx=-1, wally=-1;
    char pos;
    for (int j = m; j >=1; j--){
        for (int i = 1; i<=n ; i++) {
            if (j!=m&&getBit(arr[i][j], 2) && father[i][j] != father[i][j + 1]) {
                if (_find(i, j) + _find(i, j + 1) >= maxcombsquare) {
                    maxcombsquare = _find(i, j) + _find(i, j + 1);
                    wallx = i; wally = j; pos = 'E';
                }
            }
            if (i != 1 && getBit(arr[i][j], 1) && father[i][j] != father[i - 1][j]) {
                if (_find(i, j) + _find(i - 1, j) >= maxcombsquare) {
                    maxcombsquare = _find(i, j) + _find(i - 1, j);
                    wallx = i; wally = j; pos = 'N';
                }
            }
        }
    }
    cout << maxcombsquare << endl << wallx << " " << wally << " " << pos << endl;
}

USACO Section 1.2 Transformations

Transformations
A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations: #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees. #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees. #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees. #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image). #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3). #6: No Change: The original pattern was not changed. #7: Invalid Transformation: The new pattern was not obtained by any of the above methods. In the case that more than one transform could have been used, choose the one with the minimum number above. PROGRAM NAME: transform INPUT FORMAT Line 1: A single integer, N Line 2..N+1: N lines of N characters (each either `@' or `-'); this is the square before transformation Line N+2..2*N+1: N lines of N characters (each either `@' or `-'); this is the square after transformation SAMPLE INPUT (file transform.in) 3 @-@ --- @@- @-@ @-- --@ OUTPUT FORMAT A single line containing the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation. SAMPLE OUTPUT (file transform.out) 1 标准输入输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<string>
#include<cstring>
int n;
namespace function {
    void reverse_str(char * s) {
        int len = strlen(s);
        for (int i = 0,j=len-1; i <j; i++,j--) {
            std::swap(s[i], s[j]);
        }
    }
    bool cmp(char arr1[11][11], char arr2[11][11]) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr1[i][j] != arr2[i][j])return false;
            }
        }
        return true;
    }
    void rotate90(char arr1[11][11], char arr2[11][11]) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr2[j][n-1-i] = arr1[i][j];
            }
        }
    }
    void rotate180(char arr1[11][11], char arr2[11][11]) {
        rotate90(arr1, arr2);
        rotate90(arr2, arr1);
        std::memcpy(arr2, arr1, sizeof(char)*11*11);
    }
    void rotate270(char arr1[11][11], char arr2[11][11]) {
        rotate90(arr1, arr2);
        rotate90(arr2, arr1);
        rotate90(arr1, arr2);
    }
    void reflection(char arr1[11][11], char arr2[11][11]) {
        std::memcpy(arr2, arr1, sizeof(char) * 11 * 11);
        for (int i = 0; i < n; i++) {
            reverse_str(arr2[i]);
        }
    }
    bool combination(char arr1[11][11], char arr2[11][11],char original[11][11],char transfered[11][11]) {
        std::memcpy(arr1, original, sizeof(char) * 11 * 11);
        reflection(arr1, arr2);
        std::memcpy(arr1, arr2, sizeof(char) * 11 * 11);
        rotate90(arr1, arr2);
        if (cmp(transfered, arr2))return true;
        rotate180(arr1, arr2);
        if (cmp(transfered, arr2))return true;
        std::memcpy(arr1, original, sizeof(char) * 11 * 11);
        reflection(arr1, arr2);
        std::memcpy(arr1, arr2, sizeof(char) * 11 * 11);
        rotate270(arr1, arr2);
        if (cmp(transfered, arr2))return true;
        return false;
    }
}
namespace std {
    char arr_original[11][11],arr_transfered[11][11];
    char temp1[11][11], temp2[11][11];
    int main() {
        function::rotate90(arr_original, temp1);
        if (function::cmp(temp1, arr_transfered))return 1;
        function::rotate90(temp1, temp2);
        if (function::cmp(temp2, arr_transfered))return 2;
        function::rotate90(temp2, temp1);
        if (function::cmp(temp1, arr_transfered))return 3;
        function::reflection(arr_original, temp1);
        if (function::cmp(temp1, arr_transfered))return 4;
        if(function::combination(temp1, temp2, arr_original,arr_transfered))return 5;
        if (function::cmp(arr_original, arr_transfered))return 6;
        return 7;
    }
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::cin >> std::arr_original[i];
    }
    for (int i = 0; i < n; i++) {
        std::cin >> std::arr_transfered[i];
    }
    std::cout<<std::main();
}

洛谷 幻想迷宫

题号:1363
首先放上来一个AC代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cstring>
using namespace std;
int n, m;
char map[1505][1505];
int visited[1505][1505][2];
bool dfs(int orix, int oriy){
    int x = (orix%n+n) % n, y = (oriy%m+m) % m;
    if (map[x][y] != 'S' && map[x][y] != '.')return false;
    if (orix== visited[x][y][0]&&oriy==visited[x][y][1])return false;
    if (visited[x][y][0] != -1)return true;
    visited[x][y][0] = orix;
    visited[x][y][1] = oriy;
    if (dfs(orix + 1, oriy))return true;
    if (dfs(orix - 1, oriy))return true;
    if (dfs(orix, oriy + 1))return true;
    if (dfs(orix, oriy - 1))return true;
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    while (cin >> n >> m){
        memset(visited, -1, sizeof(visited));
        for (int i = 0; i < n; i++)cin >> map[i];
        int sx, sy;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (map[i][j] == 'S'){
                    sx = i;
                    sy = j;
                    break;
                }
            }
        }
        bool flag = dfs(sx, sy);
        if (flag)cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

题解页面:https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P1363
该思路参考:作者: mike_he 更新时间: 2016-07-27 20:57
有两种思路是错误的,在下面列举一下:
继续阅读