From 38f9dbc86318ad9b23b60979c65584c28969a728 Mon Sep 17 00:00:00 2001 From: ddthanhdat Date: Fri, 19 Jun 2015 20:15:53 +0700 Subject: [PATCH] 19-6-2015 --- 10276 - Hanoi Tower Troubles Again!.cpp | 50 ++++++++++++++ 11085 - Back to the 8-Queens.cpp | 62 +++++++++++++++++ 574 - Sum It Up.cpp | 67 +++++++++++++++++++ 624 - CD.cpp | 88 +++++++++++++++++++++++++ 628 - Passwords.cpp | 64 ++++++++++++++++++ 5 files changed, 331 insertions(+) create mode 100644 10276 - Hanoi Tower Troubles Again!.cpp create mode 100644 11085 - Back to the 8-Queens.cpp create mode 100644 574 - Sum It Up.cpp create mode 100644 624 - CD.cpp create mode 100644 628 - Passwords.cpp diff --git a/10276 - Hanoi Tower Troubles Again!.cpp b/10276 - Hanoi Tower Troubles Again!.cpp new file mode 100644 index 0000000..8018670 --- /dev/null +++ b/10276 - Hanoi Tower Troubles Again!.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; +// code// +bool kt(ll a){ + for(ll i=0;i tmp(51,0); + + +void backtrack(int left, ll i, int peg){ + if(peg>50) return; + if(left>peg){ + rs[peg]=i-1; + backtrack(left=1,i,peg+1); + }else{ + if(tmp[left]==0){ + tmp[left]=i; + backtrack(left=1,i+1,peg); + } + else if(kt(tmp[left]+i)==1) { + tmp[left]=i; + backtrack(left=1,i+1,peg); + } else backtrack(left+1,i,peg); + + } +} + +int main(){ +// freopen("ip.txt","r",stdin); + backtrack(1,1,1); + ll n; + cin >> n; + ll a; + for(int i=0;i> a; + cout << rs[a] << endl; + } + +} diff --git a/11085 - Back to the 8-Queens.cpp b/11085 - Back to the 8-Queens.cpp new file mode 100644 index 0000000..a103274 --- /dev/null +++ b/11085 - Back to the 8-Queens.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; +// code// +vector solution; +vector< vector > rs; +bool row[20]; +bool cheo1[20]; // top-left -> botum- right +bool cheo2[20]; // +void backtrack(int col){ + if(col ==9 ){ + rs.push_back(solution); + return ; + } + for(int i=1;i<=8;i++){ + int c1=i-col+10; + int c2=i+col; + if(row[i]==0 && cheo1[c1]==0 && cheo2[c2]==0){ + solution.push_back(i); + row[i]=cheo1[c1]=cheo2[c2]=1; + backtrack(col+1); + // xoa + solution.pop_back(); + row[i]=cheo1[c1]=cheo2[c2]=0; + } + } + +} +int main(){ +// freopen("ip.txt","r",stdin); + backtrack(1); + + int n,dem=1; + vector v2; + while(cin >> n){ + v2.push_back(n); + int Min=99; // khoi tao luu truong hop move it nhat + if(v2.size()==8){ + // xet cac truong hop + for(int i=0;i vector tai j trong rs + int dem=0; + for(int j=0;j<8;j++){ + if(rs[i][j]==v2[j]) dem++; + } + // + if(8-dem +#include +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; +// vector ip trong ham main +vector ip; +// value N +int N; +// vector trong ham backtrack +vector solution; +// map de tranh trung nhau +map< vector,bool > m; +// + vector tmp; +void backtrack(int left, int tong){ + if(left==ip.size() || tong >=N ){ + if(tong==N && m[solution]==0){ + bool dem=0; + for(int j=0;j> N){ + if(!N) break; + int n; cin >> n; + int a; + for(int i=0;i> a; + ip.push_back(a); + } + // + cout << "Sums of "< +#include +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; +// vector ip trong ham main +vector ip; +// value +int N; +int Max; +// vector trong ham backtrack +vector solution; +vector< vector > rs; // vector chua cac dap an + +// vector tam de cho vao rs + vector tmp; +void backtrack(int left, int tong){ + if(left==ip.size() || tong >N ){ + if(tong>N){ // do tong>N phai bo con so cuoi cung + int s=0; + for(int j=0;j> N){ + Max=0; + int n; cin >> n; + int a; + for(int i=0;i> a; + ip.push_back(a); + } + // chay de quy + backtrack(0,0); + // in output + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#define ll long long +using namespace std; +// code// +vector v; +vector chuoi; // vector chua chuoi +void xuly(string s, int left){ + if(left==s.size()){ + for(int i=0;i> n){ + cout << "--\n"; + string s; + for(int i=0;i> s; + chuoi.push_back(s); + } + int N; // xo luong rule; + cin >> N; + string rule; + for(int i=0;i> rule; + xuly(rule,0); + } + chuoi.clear(); + + } + +}