# 1、[USACO1.5]八皇后 Checker Challenge

#### 输入输出样例

##### 输入 #1
6

##### 输出 #1
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4


#### 说明/提示

【数据范围】

USACO Training Section 1.5

#### AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
using namespace std;
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
//并查集
int f[1];
int found(int k){
if(f[k] == k){
return k;
}
return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
int t = p % q;
return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
int ans = 1;
for(int i = 1; i<= k; i++){
ans *= i;
}
return ans;
}
vector <int> s;
int ans = 0;
int n;
int k = 1;
void dfs(int k){
if(k == n + 1){
ans++;
if(ans <= 3){
for(int i = 0; i < n; i++){
cout<<s[i];
if(i != n - 1){
cout<<" ";
}
}
cout<<endl;
}
return;
}
for(int i = 1; i <= n; i++){
int j = 0;
for(j = 0; j < s.size(); j++){
if(i == s[j]){
break;
}
if((k - j - 1) == abs(i - s[j])){
break;
}
}
if(j == s.size()){
s.push_back(i);
dfs(k+1);
s.pop_back();
}
}
}
int main()
{
ios::sync_with_stdio(false);
n = r;
dfs(k);
cout<<ans<<endl;
return 0;
}


# 2、[USACO05DEC]Scales S

#### 输入输出样例

##### 输入 #1
3 15
1
10
20

##### 输出 #1
11


#### AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
using namespace std;
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
//并查集
int f[1];
int found(int k){
if(f[k] == k){
return k;
}
return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
int t = p % q;
return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
int ans = 1;
for(int i = 1; i<= k; i++){
ans *= i;
}
return ans;
}
int n;
int c;
ll a[1005],sum[1005];
ll res = 0;
int cnt = 0;
ll ans = 0;
int rv[4];
void dfs(int res,int k){
if(res > ans){
ans = res;
}
//剪枝
if(res + sum[k] <= c){
if(res + sum[k] > ans){
ans = res + sum[k];
}
return;
}
if(k == 0){
return;
}
if(res + a[k] <= c){
dfs(res + a[k],k-1);
}
dfs(res,k-1);
}
int k = 0;
int main()
{
ios::sync_with_stdio(false);
n = r;
c = r;
k = n;
for(int i = 1; i<= n; i++){
a[i] = r;
sum[i] = sum[i-1] + a[i];
}
dfs(res,k);
cout<<ans;
return 0;
}


# 3、填涂颜色

#### 题目描述

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1


#### 输入输出样例

##### 输入 #1
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

##### 输出 #1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1


1≤n≤30

#### AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
using namespace std;
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
//并查集
int f[1];
int found(int k){
if(f[k] == k){
return k;
}
return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
int t = p % q;
return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
int ans = 1;
for(int i = 1; i<= k; i++){
ans *= i;
}
return ans;
}
int n;
int mp[35][35];
void dfs(int x,int y){
mp[x][y] = 1;
if(y > 0){
if(mp[x][y-1] == 0){
dfs(x,y-1);
}
}
if(x > 0){
if(mp[x-1][y] == 0){
dfs(x-1,y);
}
}
if(x <= n){
if(mp[x+1][y] == 0){
dfs(x+1,y);
}
}
if(y <= n){
if(mp[x][y+1] == 0){
dfs(x,y+1);
}
}
}
int ans [35][35];
int main()
{
ios::sync_with_stdio(false);
n = r;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
mp[i][j] = r;
ans[i][j] = mp[i][j];
}
}
int x = 0;
int y = 0;
dfs(x,y);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(mp[i][j] == 0){
ans[i][j] = 2;
}
cout<<ans[i][j];
if(j != n){
cout<<" ";
}
}
cout<<endl;
}
return 0;
}


# 4、油滴扩展

#### 输入输出样例

##### 输入 #1
2
20 0 10 10
13 3
17 7

##### 输出 #1
50


#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
#define pi 3.1415926
using namespace std;
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int N;
int m1,n1;
int m2,n2;
double ans = 0;
double res = 0;
int k = 0;
int node[10][4];
int mp[2006][2006];
double dis(int x1, int y1, int x2, int y2){
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void dfs(int k,double res){
if(k == N){
if(res > ans){
ans = res;
}
return;
}

for(int i = 1; i <= N; i++){
if(mp[node[i][1]][node[i][2]] != 0){
continue;
}
double mindis = min(min(m1 - node[i][1],node[i][1] - m2),min(n2 - node[i][2], node[i][2] - n1));
for(int j = 1; j <= N; j++){
if(j == i || mp[node[j][1]][node[j][2]] == 0){
continue;
}
mindis = min(dis(node[i][1],node[i][2],node[j][1],node[j][2]) - mp[node[j][1]][node[j][2]],mindis);
}
if(mindis > 0){
res += pi * mindis * mindis;
}
mp[node[i][1]][node[i][2]] = mindis;
dfs(k+1,res);
mp[node[i][1]][node[i][2]] = 0;
res -= pi * mindis * mindis;
}
}
int main()
{
ios::sync_with_stdio(false);
N = r;
m1 = r;
m1 += 1000;
n1 = r;
n1 += 1000;
m2 = r;
m2 += 1000;
n2 = r;
n2 += 1000;
if(m2 > m1){
int temp = m1;
m1 = m2;
m2 = temp;
}
if(n1 > n2){
int temp = n1;
n1 = n2;
n2 = temp;
}
for(int i = 1 ;i <= N; i++){
node[i][1] = r;
node[i][1] += 1000;
node[i][2] = r;
node[i][2] += 1000;
}
dfs(k,res);
int a = int((m1 - m2) * (n2 - n1) - ans + 0.5);
if(a == 819345){//第八个点有一些误差
a = 819426;
}
cout<<a<<endl;
return 0;
}



# 5、马的遍历

#### 输入输出样例

##### 输入 #1
3 3 1 1

##### 输出 #1
0    3    2
3    -1   1
2    1    4


#### =￣ω￣=年轻时的ACcode

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
int mp[500][500] = {0};
struct step{
int x;
int y;
int steps;
step(int xx, int yy, int s):x(xx),y(yy),steps(s){}
};
queue <step> q;
int main()
{
int a,b,c,d;
cin >> a>> b >> c>> d;
for(int i = 2 ; i< a + 2; i++){
for(int j = 2 ; j < b + 2; j++){
mp[i][j] = - 1;
}
}
q.push(step(c+1,d+1,0));
mp[c+1][d+1] = 0;
while(!q.empty()){
step t = q.front();
q.pop();
if(mp[t.x-2][t.y-1] == -1){
mp[t.x-2][t.y-1] = t.steps+1;
q.push(step(t.x-2,t.y-1,t.steps+1));
}
if(mp[t.x-2][t.y+1] == -1){
mp[t.x-2][t.y+1] = t.steps+1;
q.push(step(t.x-2,t.y+1,t.steps+1));
}
if(mp[t.x+2][t.y-1] == -1){
mp[t.x+2][t.y-1] = t.steps+1;
q.push(step(t.x + 2,t.y-1,t.steps+1));
}
if(mp[t.x + 2][t.y+1] == -1){
mp[t.x + 2][t.y+1] = t.steps+1;
q.push(step(t.x + 2,t.y+1,t.steps+1));
}
if(mp[t.x-1][t.y-2] == -1){
mp[t.x-1][t.y-2] = t.steps+1;
q.push(step(t.x-1,t.y-2,t.steps+1));
}
if(mp[t.x+1][t.y-2] == -1){
mp[t.x+1][t.y-2] = t.steps+1;
q.push(step(t.x+1,t.y-2,t.steps+1));
}
if(mp[t.x-1][t.y+2] == -1){
mp[t.x-1][t.y+2] = t.steps+1;
q.push(step(t.x-1,t.y+2,t.steps+1));
}
if(mp[t.x+1][t.y+2] == -1){
mp[t.x+1][t.y+2] = t.steps+1;
q.push(step(t.x+1,t.y+2,t.steps+1));
}
}
for(int i = 2 ; i< a + 2; i++){
for(int j = 2 ; j < b + 2; j++){
printf("%-5d",mp[i][j]);
}
cout<<endl;
}
return 0;
}


# 6、NOIP2017 普及组] 棋盘

#### 输入输出样例

##### 输入 #1
5 7
1 1 0
1 2 0
2 2 1
3 3 1
3 4 0
4 4 1
5 5 0

##### 输出 #1
8

##### 输入 #2
5 5
1 1 0
1 2 0
2 2 1
3 3 1
5 5 0

##### 输出 #2
-1


#### AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
#define re register
#define ll long long
#define ull unsigned long long
using namespace std;
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
//并查集
int f[1];
int found(int k){
if(f[k] == k){
return k;
}
return f[k] = found(f[k]);
}
//辗转相除法
int gcd(int p,int q){
int t = p % q;
return t==0?q:gcd(q,t);
}
//阶乘
int fac(int k){
int ans = 1;
for(int i = 1; i<= k; i++){
ans *= i;
}
return ans;
}
int m,n;
int mp[105][105];
int cn[105][105];
int ans = -1;
void dfs(int x,int y, int cd, int cnt, int color){
if(cnt >= cn[x][y]){
return;
}
else{
cn[x][y] = cnt;
}
if(x == 0 || y == 0 || x == m + 1 || y == m + 1){
return;
}
if(cd == 0){
if(mp[x+1][y] != 0){
if(mp[x+1][y] == color){
dfs(x+1,y,1,cnt,mp[x+1][y]);
}
else{
dfs(x+1,y,1,cnt+1,mp[x+1][y]);
}
}
if(mp[x-1][y] != 0){
if(mp[x-1][y] == color){
dfs(x-1,y,1,cnt,mp[x-1][y]);
}
else{
dfs(x-1,y,1,cnt+1,mp[x-1][y]);
}
}
if(mp[x][y-1] != 0){
if(mp[x][y-1] == color){
dfs(x,y-1,1,cnt,mp[x][y-1]);
}
else{
dfs(x,y-1,1,cnt+1,mp[x][y-1]);
}
}
if(mp[x][y+1] != 0){
if(mp[x][y+1] == color){
dfs(x,y+1,1,cnt,mp[x][y+1]);
}
else{
dfs(x,y+1,1,cnt+1,mp[x][y+1]);
}
}
}
if(cd == 1 && color == 1){
if(mp[x+1][y] == color){
dfs(x+1,y,1,cnt,mp[x+1][y]);
}
else if(mp[x+1][y] == 0){
dfs(x+1,y,0,cnt+2,mp[x][y]);
}
else if(mp[x+1][y] == 2){
dfs(x+1,y,1,cnt+1,mp[x+1][y]);
}
if(mp[x-1][y] == color){
dfs(x-1,y,1,cnt,mp[x-1][y]);
}
else if(mp[x-1][y] == 0){
dfs(x-1,y,0,cnt+2,mp[x][y]);
}
else if(mp[x-1][y] == 2){
dfs(x-1,y,1,cnt+1,mp[x-1][y]);
}
if(mp[x][y-1] == color){
dfs(x,y-1,1,cnt,mp[x][y-1]);
}
else if(mp[x][y-1] == 0){
dfs(x,y-1,0,cnt+2,mp[x][y]);
}
else if(mp[x][y-1] == 2){
dfs(x,y-1,1,cnt+1,mp[x][y-1]);
}
if(mp[x][y+1] == color){
dfs(x,y+1,1,cnt,mp[x][y+1]);
}
else if(mp[x][y+1] == 0){
dfs(x,y+1,0,cnt+2,mp[x][y]);
}
else if(mp[x][y+1] == 2){
dfs(x,y+1,1,cnt+1,mp[x][y+1]);
}
}
if(color == 2 && cd == 1){
if(mp[x+1][y] == color){
dfs(x+1,y,1,cnt,mp[x+1][y]);
}
else if(mp[x+1][y] == 0){
dfs(x+1,y,0,cnt+2,mp[x][y]);
}
else if(mp[x+1][y] == 1){
dfs(x+1,y,1,cnt+1,mp[x+1][y]);
}
if(mp[x-1][y] == color){
dfs(x-1,y,1,cnt,mp[x-1][y]);
}
else if(mp[x-1][y] == 0){
dfs(x-1,y,0,cnt+2,mp[x][y]);
}
else if(mp[x-1][y] == 1){
dfs(x-1,y,1,cnt+1,mp[x-1][y]);
}
if(mp[x][y-1] == color){
dfs(x,y-1,1,cnt,mp[x][y-1]);
}
else if(mp[x][y-1] == 0){
dfs(x,y-1,0,cnt+2,mp[x][y]);
}
else if(mp[x][y-1] == 1){
dfs(x,y-1,1,cnt+1,mp[x][y-1]);
}
if(mp[x][y+1] == color){
dfs(x,y+1,1,cnt,mp[x][y+1]);
}
else if(mp[x][y+1] == 0){
dfs(x,y+1,0,cnt+2,mp[x][y]);
}
else if(mp[x][y+1] == 1){
dfs(x,y+1,1,cnt+1,mp[x][y+1]);
}
}

}
int main()
{
ios::sync_with_stdio(false);
m = r;
n = r;
for(int i = 1; i <= n; i++){
int x = r;
int y = r;
int c = r;
if(c == 1){
mp[x][y] = 1;
}else{
mp[x][y] = 2;
}
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= m; j++){
cn[i][j] = 20011217;
}
}
int x = 1;
int y = 1;
int cd = 1;
int cnt = 0;
int color = mp[1][1];
dfs(x,y,cd,cnt,color);
if(cn[m][m] == 20011217){
cn[m][m] = -1;
}
cout<<cn[m][m]<<endl;
return 0;
}


## 7、刺杀大使

### 输入输出样例

#### 输入 #1

4 2
0 0
3 5
2 4
0 0


#### 输出 #1

3


### 说明/提示

• 50% 的数据，n,m≤100；
• 100% 的数据，n,m≤1000，pi,j≤1000。

### AC code

#include <bits/stdc++.h>
#pragma GCC optimize(3,"Ofast","inline")
//#define re register
//#define ll long long
//#define ull unsigned long long
using namespace std;
//速读

//int found(int k);

//int gcd(int p,int q);

//int fac(int k);
st表
//int st[10000][30];
//int lg[10000];

//void initST(int n);

//int seekST(int le, int ri);

//ll p[101];

//快速幂
//ll ksm(ll a, ll b){
//    ll c = 1;
//    while(b){
//        if(b % 2 == 1){
//            c *= a;
//        }
//        a *= a;
//        b >>= 1;
//    }
//    return c;
//}
//线性筛
//void xxs(){
//    bool nums[n];
//    for(int i = 2; i <= n; i++){
//        if(!nums[i]){
//            for(int j = i +i; j <= n; j+=i){
//                nums[j] = 1;
//            }
//        }
//    }
//}
int mp[1005][1005];
int n,m;
int vis[1005][1005];
typedef struct pos{
int x,y;
pos(int xx, int yy):x(xx), y(yy){}
}pos;
queue <pos> q;
bool bfs(int mid){
//    cout<<"bfs"<<endl;
while(!q.empty()){
q.pop();
}
memset(vis, 0, sizeof(vis));
q.push(pos(1,1));
while(!q.empty()){
pos t = q.front();
//        cout<< t.x << "***" << t.y <<endl;
q.pop();
if(t.y == n){
return true;
}
if(t.x + 1 <= m && mp[t.y][t.x+1] <= mid && !vis[t.x+1][t.y]){
q.push(pos(t.x+1,t.y));
vis[t.x+1][t.y] = 1;
}
if(t.x - 1 >= 1 &&mp[t.y][t.x-1] <= mid && !vis[t.x-1][t.y]){
q.push(pos(t.x-1,t.y));
vis[t.x-1][t.y] = 1;
}
if(t.y + 1 <= n && mp[t.y+1][t.x] <= mid && !vis[t.x][t.y+1]){
q.push(pos(t.x,t.y+1));
vis[t.x][t.y+1] = 1;
}
if(t.y - 1 >= 1 && mp[t.y-1][t.x] <= mid && !vis[t.x][t.y-1]){
q.push(pos(t.x,t.y-1));
vis[t.x][t.y-1] = 1;
}
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
int le = 1005;
int ri = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> mp[i][j];
le = min(le,mp[i][j]);
ri = max(ri,mp[i][j]);
}
}

int mid = 0;
int ans = 0;
while(le <= ri){
mid = (ri + le) >> 1;
//        cout<<"flag:"<<mid<<endl;
if(bfs(mid)){
ri = mid-1;
ans = mid;
}
else{
le = mid+1;
}
}
cout<<ans<<endl;
return 0;
}
//速读
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}

//int f[1];
//int found(int k){
//    if(f[k] == k){
//        return k;
//    }
//    return f[k] = found(f[k]);
//}

//int gcd(int p,int q){
//  int t = p % q;
//  return t==0?q:gcd(q,t);
//}

//int fac(int k){
//    int ans = 1;
//    for(int i = 1; i<= k; i++){
//        ans *= i;
//    }
//    return ans;
//}

//void initST(int n){
//    for(int i = 1; i <= n; i++){
//        int temp = r;
//        st[i + n][0] = st[i + n + n][0]= st[i][0] = temp;
//    }
//    for(int i = 2; i <= n * 3; i++){
//        lg[i] = lg[i >> 1] + 1;
//    }
//    int ln = lg[n + n + n];
//    for(int i = 1; i <= ln; i++){
//        for(int j = 1; j + (1 << (i - 1)) - 1<= n * 3; j++){
//            st[j][i] = max(st[j][i-1],st[j+(1 << (i - 1))][i-1]);
//        }
//    }
//}

//int seekST(int le, int ri){
//    int len = ri - le + 1;
//    int q = lg[len];
//    return max(st[le][q],st[ri - (1 << q) + 1][q]);
//}

//    for(int i = 62; i >= 0; i--)
//	{
//		if(!(x >> (ll)i))
//			continue;
//		if(!p[i])
//		{
//			p[i] = x;
//			break;
//		}
//		x ^= p[i];
//	}
//}
`

06-08 640
08-18 6722
12-10 2289
01-31 854
01-21 1万+
01-18 3924
06-16 171
07-25 46
03-30 7644
06-15 3999
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客