Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
merelydust committed Dec 23, 2018
1 parent 072cc3f commit 8670cc7
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***
牛跟牛之间有安全距离 即最小距离
要求这个安全距离的最大值
***/
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 100010;
int n, c;
int stall[maxn] = {0};

bool check(int tmpdis) { // 验证按照tmpdis的间隔 能否安排全部C头牛
int nCow = 0; // 已安置的牛数量
int nowRight = stall[0]; // 把第一头牛放在第一个区间
for (int i = 1; i <= n; ++i) {
if (nowRight <= stall[i]) { // 如果已安排区间的右端点在下一个栏点左边 则该右端点和该栏点构成一个新区间 可以安排进一只牛
++nCow; nowRight = stall[i] + tmpdis;
}
}
if (nCow >= c) return true; // 如果可以安排的牛数>=给定的牛数
else return false;
}

int main() {
scanf("%d %d", &n, &c);
for (int i = 1; i <= n; ++i) scanf("%d", &stall[i]);
sort(&stall[1], (&stall[1])+n);
int left = 1, right = (stall[n]-stall[0]) / (c-1), ans = 0;
int mid = 0; // 如果只有一个栏点 right=0
while (left <= right) {
mid = (left + right) / 2;
if (check(mid)) {
ans = mid; // 保存此时的mid值 因为之后进行到退出循环操作时check(mid)可能是无效的
left = mid + 1;
}
else right = mid-1; // 牛安排不完 tmpdis要拉小 右端点左移
}
printf("%d\n", ans);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
using namespace std;

int chess[4][4];
int mincnt = 17; // 棋子翻转偶数次=翻转0次 翻转奇数次=翻转1次 所以最多翻16次 初始化为非法值

void build() {
char c;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j <4; ++j) {
cin >> c;
if (c == 'w') chess[i][j] = 0;
else chess[i][j] = 1;
}
}
}

void turn(int x, int y) { // 翻转第x行y列的棋子
if (x >= 0 && x < 4 && y >= 0 && y < 4) chess[x][y] = !chess[x][y];
}

void flip(int now) { // 模拟翻转第now个棋子
int i = now / 4; // 行号
int j = now % 4; // 列号
turn(i,j);
turn(i+1,j);
turn(i,j+1);
turn(i-1,j);
turn(i,j-1);
}

bool complete() {
int cnt = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) cnt += chess[i][j];
}
if (cnt % 16 == 0) return true; // 如果cnt为16全黑或为0全白
else return false;
}

void dfs(int now, int flipCnt) { // now当前棋子 flipCnt翻转的棋子个数
if (complete()) {
if (mincnt > flipCnt) mincnt = flipCnt; // 更新mincnt
return;
}
if (now >= 16) return; // 棋子全部翻过一遍
dfs(now+1, flipCnt); // 不翻转now的分支
flip(now); // 翻转now的分支
dfs(now+1, flipCnt+1);
flip(now); // 结束翻转now的分支 还原状态
}

int main() {
build();
dfs(0, 0);
if (mincnt == 17) cout << "Impossible" << endl;
else cout << mincnt << endl;
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct node {
int x, y, data;
node* pre;
node() {
pre = NULL;
}
}matrix[5][5];

int inq[5][5] = {0};
int ans = 50; vector<node> path;
int X[4] = {1, -1, 0, 0};
int Y[4] = {0, 0, 1, -1};

bool judge(int x, int y) {
if (x < 0 || x > 4 || y < 0 || y > 4 || matrix[x][y].data || inq[x][y]) return false;
return true;
}

void bfs() {
queue<node> Q;
Q.push(matrix[0][0]);
while (!Q.empty()) {
node fro = Q.front(); Q.pop();
inq[0][0] = 1;
if (fro.x == 4 && fro.y == 4) return;
for (int i = 0; i < 4; ++i) {
int newX = fro.x + X[i], newY = fro.y+Y[i];
if (judge(newX, newY)) {
node Node; Node.x = newX; Node.y = newY;
matrix[newX][newY].pre = &(matrix[fro.x][fro.y]); // 队列传入的是副本~所以手动操作matrix
Q.push(Node); inq[Node.x][Node.y] = 1;
}
}
}
}

int main() {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
scanf("%d", &matrix[i][j].data);
matrix[i][j].x = i; matrix[i][j].y = j;
}
}
bfs();
node* ptr = &(matrix[4][4]);
while (ptr != NULL) {
path.push_back(*(ptr));
ptr = ptr->pre;
}
for (int i = path.size()-1; i >= 0; --i) {
printf("(%d, %d)\n", path[i].x, path[i].y);
}
return 0;
}

0 comments on commit 8670cc7

Please sign in to comment.