分类目录归档:搜索

洛谷 加分二叉树

题号:P1040
很有意义,想了不短时间,和一位群里的同学讨论了一下:代码如下:

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
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
int n;
struct re{
    int i;
    string s;
    re(){
       
    }
    re(int i,string s){
        this->i=i;
        this->s=s;
    }
    re operator = (const re r){
        this->i=r.i;
        this->s=r.s;
        return *this;
    }
}f[32][32];
int scores[35];
string construction(int num){
    string s="";
    while(num!=0){
        s.insert(0,1,'0'+num%10);
        num/=10;
    }
    s+=' ';
    return s;
}
re dfs(int l,int r){
    if(l==r)return re(scores[l],construction(l));
    if(l>r)return re(1,"");
    if(f[l][r].i!=0)return f[l][r];
    int mm=0;
    string s;
    for(int i=l;i<=r;i++){
        re re1=dfs(l,i-1);
        re re2=dfs(i+1,r);
        if(mm<re1.i*re2.i+scores[i]){
            mm=re1.i*re2.i+scores[i];
            s=construction(i)+re1.s+re2.s;
        }
    }
    return f[l][r]=re(mm,s);
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>scores[i];
    re re1=dfs(1,n);
    cout<<re1.i<<endl;
    cout<<re1.s;
}

Codeforces Round #494 (Div. 3) 1003 B题 Binary String Constructing 构建二进制字符串 题解

简明题意:用a个0,b个1构建出长度为n=a+b的字符串s,且对于$0 < i < n$,有$s_i != s_{i-1}$的i的个数恰为x。
当时比赛的时候没做出来,估计就是要写搜索一类的就先跳过了。后来再来写这道题目。

所以今天改了三遍。没有看任何题解。第一版:递归,第二版:递推/迭代(动规),第三版:递推(动规)+滚动数组优化。真的不知道我是怎么想起来N长时间之前背包问题里的滚动数组优化了。感觉自己特别厉害好长时间都没写了这都能想的起来。第三版程序终于有了让人没有文字说明就完全看不懂想不明白的资本啦,哈哈^v^

第一版:递归,没有任何记忆化措施,所以TLE了,然后就想着把递归换成递推。

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<iostream>
#include<algorithm>
using namespace std;
int a, b, x;
char str[105];
bool recusive(int curpos, int cura, int curb, int curx) {
    if (curpos == a + b) {
        if (cura == a && curb == b && curx == x) {
            str[curpos] = '\0';
            return true;
        }
        else return false;
    }
    if ((cura == a || curb == b)&&curx==x) {
        if (cura == a) {
            for (int i = curpos; i < a + b; i++)str[i] = '1';
            str[a + b] = '\0';
            return true;
        }
        else {
            for (int i = curpos; i < a + b; i++)str[i] = '0';
            str[a + b] = '\0';
            return true;
        }
    }
    //Assume curpos==0
    str[curpos] = '0';
    bool flag = false;
    if (curpos == 0 || str[curpos - 1] == '0')flag = recusive(curpos + 1, cura + 1, curb, curx);
    else flag = recusive(curpos + 1, cura + 1, curb, curx + 1);
    if (flag)return true;
    str[curpos] = '1';
    if (curpos == 0 || str[curpos - 1] == '1')flag = recusive(curpos + 1, cura, curb + 1, curx);
    else flag = recusive(curpos + 1, cura, curb + 1, curx + 1);
    if (flag)return true;
    return false;
}
int main() {
    cin >> a >> b >> x;
    recusive(0, 0, 0, 0);
    cout << str;
}

第二版:递推。没有任何优化,时间够了,但是内存不够MLE了。四个维度分别是a,b,x,上个字符串的最后一个数字是0还是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
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a, b, x;
string arr[101][101][201][2]; //a,b,x,
int main() {
    cin >> a >> b >> x;
    for (int i = 1; i <= a; i++)arr[i][0][0][0] = arr[i - 1][0][0][0] + '0';
    for (int i = 1; i <= b; i++)arr[0][i][0][1] = arr[0][i - 1][0][1] + '1';
    for (int k = 1; k <= x; k++) { //curx
        for (int i = 1; i <= a; i++) { //cura
            for (int j = 1; j <= b; j++) { //curb
                //Assume to place a 0
                if (arr[i - 1][j][k - 1][1] != "")arr[i][j][k][0] = arr[i - 1][j][k - 1][1] + '0';
                else if (arr[i - 1][j][k][0] != "")arr[i][j][k][0] = arr[i - 1][j][k][0] + '0';
                //Assume to place a 1
                if (arr[i][j - 1][k - 1][0] != "")arr[i][j][k][1] = arr[i][j - 1][k - 1][0] + '1';
                else if (arr[i][j - 1][k][1] != "")arr[i][j][k][1] = arr[i][j - 1][k][1] + '1';
            }
        }
    }
    if (arr[a][b][x][0] != "")cout << arr[a][b][x][0];
    else cout << arr[a][b][x][1];
}

第三版:递推+滚动数组。把第三维由201优化到只剩2。因为动规的转移方程里面只需要上一个k和当前k的数组,所以滚动数组优化一下就好了。

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<iostream>
#include<algorithm>
#include<string>
using namespace std;
int a, b, x;
string arr[101][101][2][2];
inline int trans(int num) {
    if (num % 2 == 0)return 0;
    else return 1;
}
int main() {
    cin >> a >> b >> x;
    for (int i = 1; i <= a; i++)arr[i][0][0][0] = arr[i - 1][0][0][0] + '0';
    for (int i = 1; i <= b; i++)arr[0][i][0][1] = arr[0][i - 1][0][1] + '1';
    for (int k = 1; k <= x; k++) { //curx
        for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)for(int m=0;m<=1;m++)arr[i][j][trans(k)][m]=""; //Clean the rolling array to prevent unexpected error
        for (int i = 1; i <= a; i++) { //cura
            for (int j = 1; j <= b; j++) { //curb
                //Assume to place a 0
                if (arr[i - 1][j][trans(k - 1)][1] != "")arr[i][j][trans(k)][0] = arr[i - 1][j][trans(k - 1)][1] + '0';
                else if (arr[i - 1][j][trans(k)][0] != "")arr[i][j][trans(k)][0] = arr[i - 1][j][trans(k)][0] + '0';
                else arr[i][j][trans(k)][0]="";
                //Assume to place a 1
                if (arr[i][j - 1][trans(k - 1)][0] != "")arr[i][j][trans(k)][1] = arr[i][j - 1][trans(k - 1)][0] + '1';
                else if (arr[i][j - 1][trans(k)][1] != "")arr[i][j][trans(k)][1] = arr[i][j - 1][trans(k)][1] + '1';
                else arr[i][j][trans(k)][1]="";
            }
        }
    }
    if (arr[a][b][trans(x)][0] != "")cout << arr[a][b][trans(x)][0];
    else cout << arr[a][b][trans(x)][1];
}

写了我三个小时,也是挺不容易的……继续努力!写的不清楚有问题可以在下面评论问。

NOIP2017 普及组第三道 棋盘

这题写的异常难受……先想着用动态规划,难受死了,才20分。然后后来再看题解,用记忆化BFS,写的也难受,主要是太难想,终于AC了。开心~

写了有6、7个钟头吧。挺羞愧的。。。我写这种题怎么能这么长时间呢……

所有测试点全部0ms。

成就感极高。

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
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
struct qs { //Convenient for passing parameters
    int tx, ty, os, rs, sc; //destination's coordinate (x,y) :tx,ty ;previous point::original status of color,real status of color :os,rs ;previous point's score:sc.
    qs(int tx1, int ty1, int os1, int rs1, int sc1) {
        tx = tx1; ty = ty1; os = os1; rs = rs1; sc = sc1;
    } //the init of struct
};
int m, n;
int arr[105][105], f[105][105]; //arr stores the color(status) of all coordinate; f stores the least gold the person will spend
int shiftx[4] = { 1,-1,0,0 }, shifty[4] = { 0,0,1,-1 }; //shifts for the coordinate
queue<qs> q;
void execute(qs o) {
    //Calculate New Score
    //Compare Which one is smaller
    //Only when the new one is smaller one,will the program insert the queue
    int nscore = 0x7fffffff;
    if (o.rs != -1 && arr[o.tx][o.ty] != -1) { // C2C(R2R/Y2Y/R2Y/Y2R) {"C":"Color","2":"To","R":"Red","Y":"Yellow"};
        if (o.rs == arr[o.tx][o.ty])nscore = o.sc; // R2R/Y2Y
        else nscore = o.sc + 1; // R2Y/T2R
        if (nscore < f[o.tx][o.ty] || o.tx==1&&o.ty==1) { //o.tx==1&&o.ty==1 is to create an entrance for the inital queue push
            f[o.tx][o.ty] = nscore;
            for (int i = 0; i < 4; i++)q.push(qs(o.tx + shiftx[i], o.ty + shifty[i], arr[o.tx][o.ty], arr[o.tx][o.ty], nscore));
        }
    }
    else if (o.os != -1 && arr[o.tx][o.ty] == -1) { // C2N
        if (o.os == 0 && o.sc + 2 < f[o.tx][o.ty]) { //Assume that the temporary color of current cell is Red
            f[o.tx][o.ty] = nscore = o.sc + 2;
            for (int i = 0; i < 4; i++)q.push(qs(o.tx + shiftx[i], o.ty + shifty[i], -1, 0, nscore));
            nscore += 1;
            for (int i = 0; i < 4; i++)q.push(qs(o.tx + shiftx[i], o.ty + shifty[i], -1, 1, nscore));
        }
        else if (o.os == 1 && o.sc + 2 < f[o.tx][o.ty]) { //Assume that the temporary color of current cell is Yellow
            f[o.tx][o.ty] = nscore = o.sc + 2;
            for (int i = 0; i < 4; i++)q.push(qs(o.tx + shiftx[i], o.ty + shifty[i], -1, 1, nscore));
            nscore += 1;
            for (int i = 0; i < 4; i++)q.push(qs(o.tx + shiftx[i], o.ty + shifty[i], -1, 0, nscore));
        }
    }
}
int main() {
    memset(arr, -1, sizeof(arr));
    memset(f, 0x7f, sizeof(f));
    ios::sync_with_stdio(false);
    cin >> m >> n;
    for (int i = 1; i <= n; i++) {
        int x, y, c;
        cin >> x >> y >> c;
        arr[x][y] = c;
    }
    f[1][1] = 0;
    q.push(qs(1, 1, arr[1][1], arr[1][1], 0));
    while (!q.empty()) {
        qs cur = q.front();
        q.pop();
        if (cur.tx > m || cur.ty > m || cur.tx < 1 || cur.ty < 1)continue;
        execute(cur);
    }
    cout << (f[m][m] == 0x7f7f7f7f ? -1 : f[m][m]) << endl;
}

洛谷 P1120 小木棍 [数据加强版]

写的很绝望……看了三遍所有题解仍然69……

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
#include<bits/stdc++.h>
using namespace std;
int n, sum = 0;
int lens[100] = { 0 };
bool visited[100] = { false };
int sums[100] = { 0 };
int o_min = 0, o_max;
vector<int> divisors;
int min_len;
inline bool dfs(int curpos, int curvalue, int groups){
    if (curvalue == min_len){
        if (groups == sum / min_len - 1)return true;
        else{
            int i;
            for (i = 1; i <= n; i++){
                if (!visited[i])break;
            }
            assert(i != n + 1);
            visited[i] = true;
            bool flag = dfs(i, lens[i], groups + 1);
            visited[i] = false;
            return flag;
        }
    }
    bool flag_finished = false;
    for (int i = curpos + 1; i <= n; i++){
        if (curvalue + lens[n] > min_len || curvalue + sums[i]<min_len)break;
        if (visited[i] || curvalue + lens[i] > min_len)continue;
        visited[i] = true;
        if (dfs(i, curvalue + lens[i], groups))flag_finished = true;
        visited[i] = false;
        if (flag_finished)return true;
        for (int j = i + 1; j <= n; j++){
            if (lens[j] != lens[i]){
                i = j - 1;
                break;
            }
        }
    }
    return false;
}
inline bool process(int min_length){
    min_len = min_length;
    visited[1] = true;
    bool flag = dfs(1, lens[1], 0);
    visited[1] = false;
    return flag;
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> lens[i];
        if (lens[i] > 50){
            i--;
            n--;
        }
    }
    sort(lens + 1, lens + n + 1, greater<int>());
    for (int i = 1; i <= n; i++){
        o_min = max(o_min, lens[i]);
        sum += lens[i];
    }
    for (int i = n; i >= 1; i--){
        sums[i] = sums[i + 1] + lens[i];
    }
    o_max = sum;
    for (int i = o_min; i <= o_max / 2; i++){
        if (o_max%i == 0)divisors.push_back(i);
    }
    divisors.push_back(o_max);
    for (int i = 0; i < divisors.size(); i++){
        if (process(divisors[i])){
            cout << divisors[i];
            break;
        }
    }
}

20180811日更新:已经AC。
这题真的很麻烦,之前做过一回,69分放弃了,今天又重新做,改到了78AC22TLE,又看题解里的优化终于改到了AC。
题解:https://www.luogu.org/blog/complexity/solution-p1120
https://www.luogu.org/blog/cm-nanyi2018/solution-p1120
https://www.luogu.org/blog/ogawa/solution-p1120

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
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<functional>
using namespace std;
int n,lens[70],curLen,totSum=0;
int bucket[60],lst[60],lstPtr=0;
vector<int> possibleLen;
inline int check(){
    int i;
    for(i=1;i<=lstPtr;i++)if(bucket[lst[i]]!=0)return i;
    return i;
}
inline bool dfs(int lstPos,int len){//have already added lst[lstPos] before entering this recursive function
    if(len==totSum||len==totSum-curLen)return true;
    if(len%curLen==0){
        int i=check();
        bucket[lst[i]]--;
        bool flag=dfs(i,len+lst[i]);
        bucket[lst[i]]++;
        return flag;
    }
    int curRemain=curLen-len%curLen;
    for(int i=lstPos;i<=lstPtr;i++){
        if(lst[i]>curRemain||bucket[lst[i]]==0)continue;
        bucket[lst[i]]--;
        if(dfs(i,len+lst[i])){
            bucket[lst[i]]++;
            return true;
        }
        bucket[lst[i]]++;
        if(curRemain==lst[i])return false;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    int maxV=0;
    for(int i=1;i<=n;i++){
        cin>>lens[i];
        if(lens[i]>50){
            i--;n--;
            continue;
        }
        bucket[lens[i]]++;
        maxV=max(maxV,lens[i]);
    }
    for(int i=50;i>=1;i--)if(bucket[i]!=0)lst[++lstPtr]=i;
    for(int i=1;i<=n;i++)totSum+=lens[i];
    for(int i=maxV;i<=totSum/2;i++)if(totSum%i==0)possibleLen.push_back(i);
    possibleLen.push_back(totSum);
    bucket[lst[1]]--;
    for(int i=0;i<possibleLen.size();i++){
        curLen=possibleLen[i];
        if(dfs(1,lst[1])){
            cout<<curLen;
            return 0;
        }
    }
}

洛谷10月月赛R1·浴谷八连测R1·提高组 SAC E#1 – 一道大水题 Knight

洛谷题号:P3930
2017.10.20更新:
有人在洛谷上指出了本程序的错误:测试样例:
5
….X
…..
…..
….B
..O..
存在一种4步的解法。
….4
…..
…3.
….1
..2..
但程序会输出-1。
经过检查,所给出的测试样例中四步解法经过了之前已经经过的点,这种情况是我的程序所无法处理的。因此我的程序存在漏洞,目前还没有想到什么好办法解决。。
原文章:
本来没想着这题能得分,结果这题月赛时竟然80分,后来出题人说有两个点错了,重测就100了。。。/捂脸

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<map>
#include<list>
#include<cstring>
#include<cassert>
#include<cmath>
using namespace std;
int n;
char arr[60][60]; //存放棋子
bool attack[2][60][60]; //某点是否可以被攻击(第一维没有用处,只用了第一维的1)
int dp[60][60];
int O_x, O_y, X_x_pos, X_y_pos;
int K_x[8] = {1,2,2,1,-1,-2,-2,-1};
int K_y[8] = {2,1,-1,-2,-2,-1,1,2};
int X_x[8] = {-1,0,1,-1,1,-1,0,1};
int X_y[8] = {-1,-1,-1,0,0,1,1,1};
inline void render(int x, int y, int no){ //针对坐标为(x,y)的棋子在attack数组中绘制出攻击范围
    switch (arr[x][y]){
    case 'C'://城堡
        for (int i = x + 1; i <= n; i++){
            if (arr[i][y] == '.')attack[no][i][y] = true;
            else {
                attack[no][i][y] = true;
                break;
            }
        }
        for (int i = x - 1; i >= 1; i--){
            if (arr[i][y] == '.')attack[no][i][y] = true;
            else {
                attack[no][i][y] = true;
                break;
            }
        }
        for (int i = y + 1; i <= n; i++){
            if (arr[x][i] == '.')attack[no][x][i] = true;
            else {
                attack[no][x][i] = true;
                break;
            }
        }
        for (int i = y - 1; i >= 1; i--){
            if (arr[x][i] == '.')attack[no][x][i] = true;
            else {
                attack[no][x][i] = true;
                break;
            }
        }
        break;
    case 'K'://骑士
        for (int i = 0; i < 8; i++){
            if (x + K_x[i]>0 && x + K_x[i] <= n&&y + K_y[i] > 0 && y + K_y[i] <= n){
                attack[no][x + K_x[i]][y + K_y[i]] = true;
            }
        }
        break;
    case 'B'://主教
        for (int i = 1; y - i > 0 && x + i <= n; i++){//向右上角移动
            if (arr[x + i][y - i] == '.')attack[no][x + i][y - i] = true;
            else {
                attack[no][x + i][y - i] = true;
                break;
            }
        }
        for (int i = 1; y + i <= n && x + i <= n; i++){//向右下角移动
            if (arr[x + i][y + i] == '.')attack[no][x + i][y + i] = true;
            else{
                attack[no][x + i][y + i] = true;
                break;
            }
        }
        for (int i = 1; y + i <= n && x - i > 0; i++){//向左下角移动
            if (arr[x - i][y + i] == '.')attack[no][x - i][y + i] = true;
            else{
                attack[no][x - i][y + i] = true;
                break;
            }
        }
        for (int i = 1; y - i > 0 && x - i > 0; i++){//向左上角移动
            if (arr[x - i][y - i] == '.')attack[no][x - i][y - i] = true;
            else{
                attack[no][x - i][y - i] = true;
                break;
            }
        }
        break;
    case 'Q'://皇后
        //城堡
        for (int i = x + 1; i <= n; i++){
            if (arr[i][y] == '.')attack[no][i][y] = true;
            else {
                attack[no][i][y] = true;
                break;
            }
        }
        for (int i = x - 1; i >= 1; i--){
            if (arr[i][y] == '.')attack[no][i][y] = true;
            else {
                attack[no][i][y] = true;
                break;
            }
        }
        for (int i = y + 1; i <= n; i++){
            if (arr[x][i] == '.')attack[no][x][i] = true;
            else {
                attack[no][x][i] = true;
                break;
            }
        }
        for (int i = y - 1; i >= 1; i--){
            if (arr[x][i] == '.')attack[no][x][i] = true;
            else {
                attack[no][x][i] = true;
                break;
            }
        }
        //主教
        for (int i = 1; y - i > 0 && x + i <= n; i++){//向右上角移动
            if (arr[x + i][y - i] == '.')attack[no][x + i][y - i] = true;
            else {
                attack[no][x + i][y - i] = true;
                break;
            }
        }
        for (int i = 1; y + i <= n && x + i <= n; i++){//向右下角移动
            if (arr[x + i][y + i] == '.')attack[no][x + i][y + i] = true;
            else{
                attack[no][x + i][y + i] = true;
                break;
            }
        }
        for (int i = 1; y + i <= n && x - i > 0; i++){//向左下角移动
            if (arr[x - i][y + i] == '.')attack[no][x - i][y + i] = true;
            else{
                attack[no][x - i][y + i] = true;
                break;
            }
        }
        for (int i = 1; y - i > 0 && x - i > 0; i++){//向左上角移动
            if (arr[x - i][y - i] == '.')attack[no][x - i][y - i] = true;
            else{
                attack[no][x - i][y - i] = true;
                break;
            }
        }
        break;
    case 'X'://国王
        for (int i = 0; i < 8; i++){
            if (x + X_x[i]>0 && x + X_x[i] <= n&&y + X_y[i] > 0 && y + X_y[i] <= n){
                attack[no][x + X_x[i]][y + X_y[i]] = true;
            }
        }
        break;
    case 'P'://普通士兵
        if (x - 1 > 0 && x - 1 <= n&&y + 1 > 0 && y + 1 <= n){
            attack[no][x - 1][y + 1] = true;
        }
        if (x + 1 > 0 && x + 1 <= n&&y + 1 > 0 && y + 1 <= n){
            attack[no][x + 1][y + 1] = true;
        }
        break;
    }
}
inline void render_all(){//将地图上的所有点全部重绘
    memset(attack, false, sizeof(attack));
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            switch (arr[i][j]){
            case 'O'://标记好白骑士的起始点
                O_x = i;
                O_y = j;
                break;
            case '.':
            case '\0':
                break;
            case 'X'://黑国王的终止点
                X_x_pos = i;
                X_y_pos = j;
            default:
                render(i, j, 1);
            }
        }
    }
}
void dfs(int x, int y,int step){//记忆化dfs
    if (x <= 0 || y <= 0 || x > n || y > n)return; //超出边界的情况
    if (arr[x][y] == 'X'){ //dfs到了黑国王
        if (step < dp[x][y])dp[x][y] = step;
        return;
    }
    if (attack[1][x][y])return; //刚跳到坐标x,y就被攻击了
    if (step>=dp[x][y])return; //注意,这里是重点,也是为什么DFS不会超时的原因,因为我写的这个DFS记忆化不满足无后效性,所以只要走到一点后,当前步数大于之前走到过这里的最小步数就可以退出了。
    dp[x][y] = step;
    if (arr[x][y] != '.'){ //该格子上有棋子
        char ch = arr[x][y];
        arr[x][y] = '.';
        render_all(); //把棋子吃掉以后重新绘制攻击情况
        for (int i = 0; i < 8; i++){
            if (x + K_x[i]>0 && x + K_x[i] <= n&&y + K_y[i] > 0 && y + K_y[i] <= n){
                dfs(x + K_x[i], y + K_y[i], step + 1);
            }
        }
        arr[x][y] = ch; //回溯
        render_all();
    }
    else{ //该格子上无棋子
        for (int i = 0; i < 8; i++){
            if (x + K_x[i]>0 && x + K_x[i] <= n&&y + K_y[i] > 0 && y + K_y[i] <= n){
                dfs(x + K_x[i], y + K_y[i], step + 1);
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false); //加快cin速度
    while (cin >> n){ //多组数据
        memset(arr, 0, sizeof(arr));
        memset(attack, false, sizeof(attack));
        memset(dp, 0x7f, sizeof(dp));
        for (int i = 1; i <= n; i++){
            cin >> arr[i] + 1;
        }
        render_all(); //先进行整幅地图攻击情况的绘制
        dfs(O_x, O_y, 0);
        if (dp[X_x_pos][X_y_pos] == 0x7f7f7f7f){
            cout << -1 << endl;
        }
        else{
            cout << dp[X_x_pos][X_y_pos] << endl;
        }
    }
    return 0;
}

洛谷/USACO 魔板 Magic Squares

洛谷题号:P2730
THUOJ上也有这道题目,题目输入输出格式略微有不同,基本原理相同。但是THUOJ不能使用任何数据结构,全部都要自己写。

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
struct status{
    int arr[9];
};
string s_result;
template <typename T> struct node{
    T data;
    node<T> * next;
};
template <typename T> struct list{
    node<T> * head;
    int _size;
    list(){
        _size = 0;
        head = NULL;
    }
    ~list(){
        node <T> * current = head;
        node <T> * next;
        while (current != NULL){
            next = current->next;
            delete current;
            current = next;
        }
    }
    void insert(T & data){
        _size++;
        node<T> * ptr = new node<T>;
        ptr->data = data;
        ptr->next = head;
        head = ptr;
    }
    bool search(T & target){
        node<T> * ptr = head;
        while (ptr != NULL){
            if (ptr->data == target)return true;
            ptr = ptr->next;
        }
        return false;
    }
};
list<int> hashtable[50021];
inline int h(const int m){
    return (2 * m + 29) % 50021;
}
inline int status2int(status m){
    int t = 0;
    for (int i = 1; i <= 8; i++){
        t *= 10;
        t += m.arr[i];
    }
    return t;
}
inline status int2status(int t){
    status m;
    for (int i = 8; i >= 1; i--){
        m.arr[i] = t % 10;
        t /= 10;
    }
    return m;
}
inline int change(int status_m, char type){
    status m = int2status(status_m);
    int t;
    switch (type){
    case 'A':
        swap(m.arr[1], m.arr[8]);
        swap(m.arr[2], m.arr[7]);
        swap(m.arr[3], m.arr[6]);
        swap(m.arr[4], m.arr[5]);
        break;
    case 'B':
        t = m.arr[4];
        for (int i = 4; i >= 2; i--)m.arr[i] = m.arr[i - 1];
        m.arr[1] = t;
        t = m.arr[5];
        for (int i = 5; i <= 7; i++)m.arr[i] = m.arr[i + 1];
        m.arr[8] = t;
        break;
    case 'C':
        t = m.arr[2];
        m.arr[2] = m.arr[7];
        m.arr[7] = m.arr[6];
        m.arr[6] = m.arr[3];
        m.arr[3] = t;
        break;
    }
    return status2int(m);
}
int BFS(int m){
    queue<int> q;
    queue<int> qi;
    queue<string> qs;
    q.push(12345678);
    qi.push(0);
    qs.push("");
    while (!q.empty()){
        int t = q.front();
        q.pop();
        int time = qi.front();
        qi.pop();
        string str = qs.front();
        qs.pop();
        if (t == m){
            s_result = str;
            return time;
        }
        else if (time > 30){
            continue;
        }
        else{
            int s = change(t, 'A');
            if (!hashtable[h(s)].search(s)){
                hashtable[h(s)].insert(s);
                q.push(s); qi.push(time + 1); qs.push(str + 'A');
            }
            s = change(t, 'B');
            if (!hashtable[h(s)].search(s)){
                hashtable[h(s)].insert(s);
                q.push(s); qi.push(time + 1); qs.push(str + 'B');
            }
            s = change(t, 'C');
            if (!hashtable[h(s)].search(s)){
                hashtable[h(s)].insert(s);
                q.push(s); qi.push(time + 1); qs.push(str + 'C');
            }
        }
    }
    return -1;
}
int main(){
    ios::sync_with_stdio(false);
    status m;
    for (int i = 1; i <= 8; i++){
        cin >> m.arr[i];
    }
    cout << BFS(status2int(m)) << endl;
    cout << s_result << endl;
   
}

洛谷 穿越栅栏 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;
}

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;
}

洛谷 幻想迷宫

题号: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
有两种思路是错误的,在下面列举一下:
继续阅读