This is quite an easy problem. But it took me long time to realize that to determine the number of total game segments is nothing but counting the direction changes to reach the destination. #include <iostream> #include <cstdio> #include <climits> #include <queue> #include <cstring> #include <string> using namespace std; int w,h; int SEGMENT; int gr[80][80];
Month: October 2015
677
#include <iostream> #include <climits> #include <cstdio> #include <cstring> #include <vector> #include <string> using namespace std; int gr[15][15]; bool found = false; vector<int> vb; int node,length; int backtrack(int st,int len,bool visit[]) { if(len==length) { found=1; printf("("); for(int i=0;i<vb.size();i++) { printf("%d%s",vb[i]+1,i==vb.size()-1?")\n":","); } return 0; } for(int i=0;i<node;i++) { if(gr[st][i]==1 && visit[i]==0) { visit[i]=1; vb.push_back(i); backtrack(i,len+1,visit); visit[i]=0; vb.pop_back();
633
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <queue> using namespace std; int stx,sty,edx,edy; int gr[405][405]; int dirx[8]={-2,-2,2,2,1,1,-1,-1}; int diry[8]={1,-1,1,-1,2,-2,2,-2}; int dx[4]={2,2,-2,-2}; int dy[4]={-2,2,2,-2}; int n; int bfs() { queue<int> q; int x,y; //int visit[405][405]; //memset(visit,0,sizeof(visit)); int moveTyp=0,move; int step=0,stepCount; q.push(stx);q.push(sty);q.push(moveTyp);q.push(step); if(gr[stx][sty]==1) return -1; else if(gr[edx][edy]==1) return -1; while(!q.empty()) { x = q.front();q.pop(); y
627
#include <iostream> #include <cstdio> #include <sstream> #include <vector> #include <string> #include <cstring> #include <queue> using namespace std; vector<int> vb[305]; vector<int> par; bool bfs(int path[],int st,int ed) { int visit[400]; for(int i=0;i<400;i++) visit[i]=0; queue<int> q; q.push(st); visit[st]=1; int a,b; if(st==ed) return 1; while(!q.empty()) { a = q.front();q.pop(); if(a==ed) return 1; for(int i=0;i<vb[a].size();i++) { b = vb[a][i];
615
The problem is to finding a tree from a given graph.The main challenge is that the value of the node is not provided. So adjacency matrix can not be used to solve the problem. To define a tree for a directed graph : total edges = total vertices -1;root can not be more than one,
599
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; int parent[26]; int countNodes[26]; int getparent(int n) { if(parent[n]==n) return n; else return parent[n]=getparent(parent[n]); } void unite(int a,int b) { parent[getparent(a)]=getparent(b); } int main() { int t; string str; scanf("%d",&t); getchar(); while(t–) { str=""; for(int i=0;i<26;i++) { parent[i]=i; countNodes[i]=0; } while(getline(cin,str),str[0]!='*') { unite(str[1]-'A',str[3]-'A'); }
590
#include <iostream> #include <cstdio> #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <queue> #include <cstdlib> #define INF 1000000000 #define ll long long using namespace std; int n,k; int flight_schedule_cost[11][11][40];//flight_schedule_cost[city][tocity][dayno]=cost of that day frm ct1 to cty 2 int periodOfCity[11][11]; ll dp[1010][15];//ll dp[day][citypath]; int kase=1; int costFromTheCity(int day,int startCity,int destination_city) { int schedulePeriod = periodOfCity[startCity][destination_city];
11734
#include <cstdio> #include <vector> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int main() { int t,kase=1; string org,ans,tmp1,tmp2; scanf("%d",&t); getchar(); while(t–) { getline(cin,ans); getline(cin,org); if(org.size()==ans.size()) { bool f=0; for(int i=0;i<org.size();i++) { if(org[i]!=ans[i]) { f=1;break; } } if(f) printf("Case %d: Wrong Answer\n",kase++); else printf("Case %d: Yes\n",kase++); } else { tmp1="";tmp2=""; for(int i=0;i<org.size();i++) { if(org[i]!='
11934
Have some idea about Remainder Theorem, not necessary to solve the problem but it is good to revise some theorem. #include <iostream> #include <cstdio> #include <sstream> #include <string> #define ll long long using namespace std; int main() { int a,b,c,d,L; while(scanf("%d%d%d%d%d",&a,&b,&c,&d,&L)) { if(a==0 && b==0 && c==0 && d==0 && L==0) break; if(a==0 && b==0
11936
#include <iostream> #include <cstdio> #include <sstream> #include <string> using namespace std; int main() { int n,a,b,c; scanf("%d",&n); while(n–) { scanf("%d%d%d",&a,&b,&c); bool OK=1; if(a>=(b+c)) OK=0; if(b>=(a+c)) OK=0; if(c>=(a+b)) OK=0; if(OK) printf("OK\n"); else printf("Wrong!!\n"); } return 0; }