USACO the clocks

在摸索这个是枚举 还是 DFS……



/*
ID: wangxin12
PROG: clocks
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <bitset>


using namespace std;


bool Cmp(int & a, int & b) {
	if(a >= b) return true;
	return false;
}

template<typename T>
int Partition(vector<T> & f, int start, int finish, bool (cmp)(T & a, T & b)) {
	T key = f[start];
	int left = start + 1;
	int right = finish;

	while(true) {
		
		while(right > left && cmp(f[right],key)) right--;
		while(left < right && !cmp(f[left],key)) left++;
		if(left == right) break;
		T temp = f[left];
		f[left] = f[right];
		f[right] = temp;
	}

	if(cmp(f[left],key)) return start;
	f[start] = f[left];
	f[left] = key;
	return left;
}

template<typename T>
void Qsort(vector<T> & f, int start, int finish) { 
	if(start >= finish) return;
	int boundary = Partition(f, start, finish, Cmp);
	Qsort(f, start, boundary - 1);
	Qsort(f, boundary + 1, finish);
}

//global variable
struct Clocks {
	int pt[10];
	vector<int> path;
	int limits[10];
};

int depth_min = 28;

Clocks init_clocks;
Clocks final_clocks;

int flag[10][5] = {
	 {},
	 {1,2,4,5},
	 {1,2,3},
	 {2,3,5,6},
	 {1,4,7},
	 {2,4,5,6,8},
	 {3,6,9},
	 {4,5,7,8},
	 {7,8,9},
	 {5,6,8,9}
};

void DFS(Clocks clocks, int r) {
	clocks.limits[r]++;
	if(clocks.limits[r] > 3) return;
	if(clocks.path.size() >= depth_min) return;

	
	
	//rotate and add path
	for(int i = 0; i < 5; i++) {
		int t = flag[r][i];
		clocks.pt[t] = (clocks.pt[t] + 3) % 12;
	}
	clocks.path.push_back(r);
	

	//check all 0
	bool bflag = true;
	for(int i = 1; i <= 9; i++) {
		if(clocks.pt[i] != 0) {
			bflag = false;
			break;
		}
	}
	if(bflag) {
		depth_min = clocks.path.size();
		//将clocks深拷贝到final_clocks里面去
		for(int t = 1; t <= 9; t++)		{ 
			final_clocks.pt[t] = clocks.pt[t];
			final_clocks.limits[t] = clocks.limits[t];
		}
		final_clocks.path.clear();
		for(int t = 0; t < clocks.path.size(); t++) final_clocks.path.push_back(clocks.path[t]);

		return;
	}

	//dfs
	for(int m = r; m <= 9; m++) {
		DFS(clocks, m);
	}

	

}

int main() {
	ifstream fin("in.txt");
	ofstream fout("out.txt");

	//初始化init_clocks
	for(int i = 1; i <= 9; i++) {
		int t = 0;
		fin>>t;
		//cout<<t % 12<<"  ";
		init_clocks.pt[i] = t % 12;
		init_clocks.limits[i] = 0;
	}

	/*flag二维数组,未标记的位自动初始化为0
	for(int i = 0; i < 10; i++) {
		for(int j = 0; j < 6; j++) {
			cout<<flag[i][j]<<" ";
		}
		cout<<endl;
	} */

	//DFS
	for(int i = 1; i <= 9; i++) {
		DFS(init_clocks, i);
	}


	//排序
	Qsort(final_clocks.path, 0, final_clocks.path.size() - 1);
	
	//输出结果
	int i = 0;
	for(i = 0; i < final_clocks.path.size() - 1; i++) {
		fout<<final_clocks.path[i]<<" ";
	}
	fout<<final_clocks.path[i]<<endl;

	fin.close();
	fout.close();

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值