Skip to content

Commit

Permalink
19-6-2015
Browse files Browse the repository at this point in the history
  • Loading branch information
ddthanhdat committed Jun 19, 2015
1 parent 8c6f2d2 commit 38f9dbc
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 10276 - Hanoi Tower Troubles Again!.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// code//
bool kt(ll a){
for(ll i=0;i<a;i++)
if(i*i==a) return 1;
return 0;
}
ll rs[51];
vector<ll> 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<n;i++){
cin >> a;
cout << rs[a] << endl;
}

}
62 changes: 62 additions & 0 deletions 11085 - Back to the 8-Queens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// code//
vector<int> solution;
vector< vector<int> > 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<int> 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<rs.size();i++){ // vector long vector rs[j] -> 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<Min) Min=8-dem;
}
cout << "Case " << dem++ <<": "<<Min << endl;
v2.clear();
}


}

}
67 changes: 67 additions & 0 deletions 574 - Sum It Up.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// vector ip trong ham main
vector<int> ip;
// value N
int N;
// vector trong ham backtrack
vector<int> solution;
// map de tranh trung nhau
map< vector<int>,bool > m;
//
vector<int> 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<solution.size();j++){
if(dem) cout << "+";
cout << solution[j];
dem=1;
}
cout << endl;
m[solution]=1;
}
return;
}
for(int i=left;i<ip.size();i++){
solution.push_back(ip[i]);
backtrack(1+i,tong+ip[i]);

// xoa tai cho
solution.pop_back();
}
}
int main(){
// freopen("ip.txt","r",stdin);
// freopen("rs.txt","w",stdout);
while(cin >> N){
if(!N) break;
int n; cin >> n;
int a;
for(int i=0;i<n;i++){
cin >> a;
ip.push_back(a);
}
//
cout << "Sums of "<<N <<":"<<endl;

backtrack(0,0);
// in output
if(m.size()==0) cout << "NONE"<<endl;

//reset
ip.clear();
m.clear();
}


}
88 changes: 88 additions & 0 deletions 624 - CD.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// vector ip trong ham main
vector<int> ip;
// value
int N;
int Max;
// vector trong ham backtrack
vector<int> solution;
vector< vector<int> > rs; // vector chua cac dap an

// vector tam de cho vao rs
vector<int> 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<solution.size()-1;j++){ //j<solution.size()-1 ; khong lay so cuoi cung
s+=solution[j];
tmp.push_back(solution[j]);
}
if(Max<s) Max=s;
rs.push_back(tmp);
tmp.clear();
return;
}
int s=0;
for(int j=0;j<solution.size();j++){
s+=solution[j];
}
if(Max<s) Max=s;
rs.push_back(solution);
return;
}

for(int i=left;i<ip.size();i++){


solution.push_back(ip[i]);
backtrack(1+i,tong+ip[i]);

// xoa tai cho
solution.pop_back();

}
}
int main(){
// freopen("ip.txt","r",stdin);
// freopen("rs.txt","w",stdout);
while(cin >> N){
Max=0;
int n; cin >> n;
int a;
for(int i=0;i<n;i++){
cin >> a;
ip.push_back(a);
}
// chay de quy
backtrack(0,0);
// in output
for(int i=0;i<rs.size();i++){
int ss=0;
for(int j=0;j<rs[i].size();j++){
ss+=rs[i][j];
}
if(ss==Max){
for(int j=0;j<rs[i].size();j++)
cout << rs[i][j]<< " ";
break;
}
}
cout << "sum:" << Max << endl;
//reset
ip.clear();
rs.clear();

}


}
64 changes: 64 additions & 0 deletions 628 - Passwords.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// code//
vector<string> v;
vector<string> chuoi; // vector chua chuoi
void xuly(string s, int left){
if(left==s.size()){
for(int i=0;i<v.size();i++){
cout << v[i];
}
cout << endl;
return ;
}
if(s[left]=='#'){
for(int i=0;i<chuoi.size();i++){
v.push_back(chuoi[i]);
xuly(s,left+1);
//xoa
v.pop_back();
}
}
if(s[left]=='0'){
for(int i=0;i<10;i++){
string a;
a+=(char)(i+'0');
v.push_back(a);
a.clear();
xuly(s,left+1);
// xoa
v.pop_back();
}
}
}
int main(){
// freopen("ip.txt","r",stdin);

int n;
while(cin >> n){
cout << "--\n";
string s;
for(int i=0;i<n;i++){
cin >> s;
chuoi.push_back(s);
}
int N; // xo luong rule;
cin >> N;
string rule;
for(int i=0;i<N;i++){
cin >> rule;
xuly(rule,0);
}
chuoi.clear();

}

}

0 comments on commit 38f9dbc

Please sign in to comment.