这周也算是比较清闲的,没有那么多的课,也就留出了蛮多时间可以用来在这里敲敲敲;
快期中考试了,下周还是要复习一下的,哎,真烦
今天的三个代码后两个都参考了人家挺多的,这里有必要说明一下,给个链接吧:
[后两道题的引用:(http://blog.csdn.net/thudaliangrx/article/details/50747673#t12)]
感谢这位大神教会了我这么多的东西,谢谢
代码如下:
6-10:
#include<cstdio>
#include<iostream>
#include<list>
#include<deque>
#include<vector>
using namespace std;
list<int>hand;
vector<deque<int>>a;
int legal(int x1, int x2, int x3) {
int sum = x1 + x2 + x3;
if (sum == 10 || sum == 20 || sum == 30)
return sum;
else
return 0;
}
void init() {
int i = 0;
deque<int>k;
while (i++ < 7) {
a.push_back(k);
}
}
int sum1;
int solve() {
while (1) {
if (a.size() == 0)
{
cout << "win: " << sum1;
return 1;
}
for (int i = 0; i < a.size(); i++) {
if (hand.size() == 0)
{
cout << "lose" << sum1;
return 1;
}
a[i].push_back(hand.front());
hand.pop_front();
int size = a[i].size();
if (size >= 3)
{
int sum;
if (sum = legal(a[i][0], a[i][1], a[i][size - 1])) {
hand.push_back(a[i][0]);
hand.push_back(a[i][1]);
hand.push_back(a[i][size - 1]);
a[i].pop_front();
a[i].pop_front();
a[i].pop_back();
sum1 += sum;
}
else if (sum = legal(a[i][0], a[i][size - 1], a[i][size - 2])) {
hand.push_back(a[i][0]);
hand.push_back(a[i][size - 2]);
hand.push_back(a[i][size - 1]);
a[i].pop_front();
a[i].pop_back();
a[i].pop_back();
sum1 += sum;
}
else if (sum = legal(a[i][size - 1], a[i][size - 2], a[i][size - 3])) {
hand.push_back(a[i][size - 3]);
hand.push_back(a[i][size - 2]);
hand.push_back(a[i][size - 1]);
a[i].pop_back();
a[i].pop_back();
a[i].pop_back();
sum1 += sum;
}
}
vector<deque<int>>::iterator it = a.begin();
if (a[i].size() == 0) {
advance(it, i);
a.erase(it);
}
else
continue;
}
}
}
int main() {
int i = 52, j;
while (i--) {
cin >> j;
hand.push_back(j);
}
init();
for (i = 0; i < 7; i++) {
j = hand.front();
a[i].push_back(j);
hand.pop_front();
}
for (i = 0; i < 7; i++) {
a[i].push_front(hand.front());
hand.pop_front();
}
solve();
return 0;
}
6-11:
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
const int N = 100;
int n;
vector<int>bfs, dfs;
vector<int>children[N];
set<int>store;
void solve() {
vector<int>::iterator it = bfs.begin() + 1, itt, left_bound, ittt;
itt = left_bound = dfs.begin();
store.insert(bfs[0]);
while (it != bfs.end()) {
itt = find(dfs.begin(), dfs.end(), *it);
left_bound = itt;
if (itt != dfs.end()) {
int i = distance(dfs.begin(), itt) - 1;
while (i >= 0) {
if (store.count(dfs[i])) {
children[dfs[i]].push_back(*itt);
break;
}
i--;
}
}
it++;
ittt = find(left_bound, dfs.end(), *it);
if (ittt != dfs.end()) {
int j = distance(dfs.begin(), ittt) - 1;
while (j >= 0) {
if (store.count(dfs[j])) {
children[dfs[j]].push_back(*ittt);
break;
}
j--;
}
}
else
continue;
it++;
store.insert(*itt);
store.insert(*ittt);
}
}
int main() {
int i, j;
cin >> n;
for (i = 0; i < n; i++) {
cin >> j;
bfs.push_back(j);
}
for (i = 0; i < n; i++) {
cin >> j;
dfs.push_back(j);
}
solve();
for (i = 1; i <= n; i++) {
cout << i << " : ";
for (j = 0; j < children[i].size(); j++) {
cout << children[i][j]<<" ";
}
cout << endl;
}
return 0;
}
6-12:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<deque>
using namespace std;
int r2,c2, r1, c1;//前面是总的,后面是起点
int a[10][10], INF = 0x3f3f3f3f;
int r[6][6];
int dir[4][2] = { { 1, 0 },{ 0, 1 },{ -1, 0 },{ 0, -1 } };
struct node {
int x, y, t, f, d;
node *pre;
node() :x(0), y(0), t(0), f(0), d(0),pre(NULL){}
};
node* _begin;
node map[10][10][7][7];
//这个map是用来记录每个格子的状态的
void init_mp()
{
for (int x = 1; x <= r2; x++) {
for (int y = 1; y <= c2; y++) {
scanf_s("%d", &a[x][y]);
for (int t = 1; t <= 6; t++) {
for (int f = 1; f <= 6; f++) {
map[x][y][t][f].x = x;
map[x][y][t][f].y = y;
map[x][y][t][f].t = t;
map[x][y][t][f].f = f;
map[x][y][t][f].d = INF;
map[x][y][t][f].pre = NULL;
}
}
}
}
}
bool legal(int x, int y) {
return x >= 0 && x < r2&&y >= 0 && y < c2;
}
void init_right()
{
memset(r, 0, sizeof(r));
r[6][2] = r[2][1] = r[1][5] = r[5][6] = 4;
r[6][5] = r[5][1] = r[1][2] = r[2][6] = 3;
r[6][3] = r[3][1] = r[1][4] = r[4][6] = 2;
r[6][4] = r[4][1] = r[1][3] = r[3][6] = 5;
r[3][2] = r[2][4] = r[4][5] = r[5][3] = 6;
r[3][5] = r[5][4] = r[4][2] = r[2][3] = 1;
}
node* bfs() {
queue<node*>p;
p.push(_begin);
bool first = true;
while (p.size()) {
node *mid = p.front();
p.pop();
if (!first) {
if (mid->x == _begin->x&&mid->y == _begin->y)
return mid;
}
else
first = false;
for (int i = 0; i < 4; i++) {
int nx = mid->x + dir[i][0];
int ny = mid->y + dir[i][1];
if (legal(nx, ny) && (a[nx][ny] == 0 || a[nx][ny] == mid->t)) {
int nt, nf, t = mid->t, f = mid->f;
if (i == 0) { //down
nt = 7 - f; nf = t;
}
else if (i == 1) { //right
nt = 7 - r[t][f]; nf = f;
}
else if (i == 2) { //up
nt = f; nf = 7 - t;
}
else if (i == 3) { //left
nt = r[t][f]; nf = f;
}
node*mid2 = &map[nx][ny][nt][nf];
if (mid2->d == INF || mid2 == _begin) {
mid2->d = mid->d + 1;
mid2->pre = mid;
p.push(mid2);
}
}
}
}
return NULL;
}
int main() {
cin >> r2 >> c2;
int i, j, top, front;
cin >> top >> front >> r1 >> c1;
init_mp();
init_right();
_begin = &map[r1][c1][top][front];
node* root = bfs();
deque<node*>way;
while (root!=_begin) {
way.push_front(root);
root = root->pre;
}
cout << '(' << _begin->x << "," << _begin->y << ")" << " ";
for (int k = 0; k < way.size(); k++) {
cout << "(" << way[k]->x << "," << way[k]->y << ")" << " ";
}
return 0;
}