上周写的,今天也写了一个,不过一直没调出来,改天发上来。
代码如下:
带宽:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
set<int>neigh[26];
char tos[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vector<int>tar;
vector<int>store;
int k = 8;
//操他妈迭代器用的飞起
int search() {
int len, dis=0 ;
vector<int>::iterator it;
for (vector<int>::iterator i = tar.begin(); i != tar.end(); i++) {
for (set<int>::iterator j = neigh[*i].begin(); j !=neigh[*i].end(); j++) {
if (neigh[*i].size() >= k)
return 0;
it = find(tar.begin(), tar.end(), *j);
len = abs(it - i);
if (len >= k)
return 0;
if (len > dis)
dis = len;
}
}
return dis;
}
int main()
{
char ch = getchar();
int a = ch - 'A';
set<int>mid;
//记录谁的相邻节点有谁
while ((ch = getchar()) != '#') {
if (ch == ':')
continue;
else if (ch == ';') {
ch = getchar();
a = ch - 'A';
mid.insert(ch - 'A');
}
else {
neigh[a].insert(ch - 'A');
neigh[ch - 'A'].insert(a);
mid.insert(ch-'A');
}
}
copy(mid.begin(), mid.end(), back_inserter(tar));
int kase;
do {
kase = search();
if (kase && kase < k) {
k = kase;
store.assign(tar.begin(), tar.end());
}
} while (next_permutation(tar.begin(), tar.end()));
for (auto i : store) {
cout << tos[i] << " ";
}
cout << "-> " << k;
return 0;
}
- 宝箱:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int s1, v1, s2, v2, n, kase, j;
long long assest;
int main() {
cin >> kase;
while (j++<kase) {
scanf_s("%d%d%d%d%d", &n, &s1,&v1,&s2, &v2);
if (n / s1 < 65536 || n / s2 < 65536) {
assest = 0;
if (n / s1 < n / s2)
for (long long i = 0; i <= n / s1; i++)
assest = max((n - s1*i) / s2*v2 + i*v1, assest);
else
for (long long i = 0; i <= n / s2; i++) {
assest = max((n - s2*i) / s1*v1 + i*v2, assest);
}
}
else {
assest = 0;
if (s1*v2 < s2*v1) { swap(s1, s2); swap(v1, v2); }
for (long long i = 0; i <= s2 - 1; i++) {
assest = max((n - i*s1) / s2*v2 + i*v1, assest);
}
}
printf("Case #%d: %lld\n", j, assest);
}
return 0;
}
- 例题7-2
#include<cstdio>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int bd = 105;
const char tos[] = "news";
int g[250][250], v[21];
int dir[4][2] = { {1,0},{0,1},{0,-1},{-1,0} };
vector<int>path;
pair<int, int>_begin = { 0,0 };
int n, k, path_num;
void find_path() {
for (int i = 0; i < path.size(); i++) {
cout << tos[i] << " ";
}
cout << endl;
}
bool legal(int x, int y) {
return abs(x) < bd && abs(y) < bd;
}
void dfs(pair<int, int>u) {
if (path.size() == n) {
if (u == _begin) {
path_num++;
find_path();
return;
}
}
int step = path.size();
for (int i = 0; i < 4; i++) {
if (step && (path[step - 1] + 1) % 4 / 2 == (i + 1) % 4 / 2)
continue;//判断该往哪里转弯
pair<int, int>p = u;
bool flag = true;
for (int j = 0; j < step; j++) {
p.first += dir[i][0];
p.second += dir[i][1];
if (legal(p.first, p.second) || g[p.first + bd][p.second + bd] == -1) {
flag = false;
break;
}
}
if (flag&&g[p.first + bd][p.second + bd] != 1) {
path.push_back(i);
g[p.first + bd][p.second + bd] = 1;
dfs(p);
//当程序运行到这一步的时候,说明递归调用的完毕
//他运行出结果了,最好,接下来的操作也不会影响他什么
//但是他如果没有运行出结果,接下来的操作就是非常必要的了
g[p.first + bd][p.second + bd] = 0;//取消之前的标记
path.pop_back();//等价于path。resize(),都是剔除最后之前插入的元素
}
}
}
int main() {
cin >> n >> k;
int a, b;
memset(g, 0, sizeof(g));
for (int i = 0; i < k; i++) {
scanf_s("%d%d", &a, &b);
g[a + bd][b + bd] = -1;
}
int step = 1;
pair<int, int>u = { 0,0 };
dfs(u);
cout << path_num;
return 0;
}