prakash.4's blog

By prakash.4, history, 5 years ago, In English

I don't know why i am getting wrong answer. Could anyone help me in finding out what's wrong in this code.

This is the link to the question. https://www.hackerearth.com/problem/algorithm/move-the-knight/

Below is my code.

include <bits/stdc++.h>

define mod 1000000007

using namespace std;

void f(int r,int c,vector<vector> &visited,int &cnt) { if(r >= 1 && r <= 8 && c >= 1 && c <= 8 && visited[r][c] == false) { visited[r][c] = true; cnt++; f(r+1,c+2,visited,cnt); f(r+1,c-2,visited,cnt); f(r-1,c+2,visited,cnt); f(r-1,c-2,visited,cnt); f(r+2,c+1,visited,cnt); f(r-2,c+1,visited,cnt); f(r+2,c-1,visited,cnt); f(r-2,c-1,visited,cnt); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin>>t; while(t--) { int r,c,cnt = -1; cin>>r>>c; vector< vector > visited(9); for(int i = 1; i <= 8; ++i) { vector temp(9); visited[i] = temp; } f(r,c,visited,cnt); cout<<cnt<<"\n"; for(int i = 1; i <= 8; ++i) for(int j = 1; j <= 8; ++j) if(visited[i][j] == true) cout<<i<<" "<<j<<" "; cout<<"\n"; } return 0; }

Thanks.

Full text and comments »

  • Vote: I like it
  • -31
  • Vote: I do not like it

By prakash.4, history, 5 years ago, In English

Could anyone please help me find out what's wrong in my code. I'm getting wrong answer. Probably i didn't understand the question properly.

This is the link to the question. https://www.spoj.com/problems/AKBAR/

Below is my code.

include<bits/stdc++.h>

using namespace std; void dfs(vector<vector> &city,vector &visited,int s,int strength,map<int,int> &soldier) { visited[s] = true; for(int i = 0; i < city[s].size(); ++i) { if(visited[city[s][i]] == false && strength > 0 && soldier.find(city[s][i]) == soldier.end()) dfs(city,visited,city[s][i],strength-1,soldier); } }

int main() { int t; scanf("%d",&t); while(t--) { int n,r,m; scanf("%d%d%d",&n,&r,&m); vector<vector> city(n+1); vector visited(n+1); while(r--) { int a,b; scanf("%d%d",&a,&b); city[a].push_back(b); city[b].push_back(a); } map<int,int> soldier; while(m--) { int k,s; scanf("%d%d",&k,&s); soldier.insert({k,s}); } for(auto it = soldier.begin(); it != soldier.end(); ++it) dfs(city,visited,it->first,it->second,soldier); int i; for(i = 1; i <= n; ++i) if(visited[i] == false) { printf("No\n"); break; } if(i > n) printf("Yes\n"); } return 0; }

Thanks.

Full text and comments »

  • Vote: I like it
  • -30
  • Vote: I do not like it