Friday, February 14, 2020

Numbers

#include<iostream>
using namespace std;
void swap(char*a,char*b){
  char c;
  c=*a;
  *a=*b;
  *b=c;
}
void permute(string s,int i,int n)
{
  if(i==n)
    cout<<s<<"\n";
  else{
    for(int j=i;j<s.length();j++){
      swap(s[i],s[j]);
      permute(s,i+1,n);
      swap(s[i],s[j]);
    }
  }
}
int main(){
  string s;
  cin>>s;
  permute(s,0,s.length());
  return 0;}

string

#include<iostream>
using namespace std;
void swap(char*a,char*b){
  char c;
  c=*a;
  *a=*b;
  *b=c;
}
void permute(string s,int i,int n)
{
  if(i==n)
    cout<<s<<"\n";
  else{
    for(int j=i;j<s.length();j++){
      swap(s[i],s[j]);
      permute(s,i+1,n);
      swap(s[i],s[j]);
    }
  }
}
int main(){
  string s;
  cin>>s;
  permute(s,0,s.length());
  return 0;}

Alphabets

#include <stdio.h>
#include <string.h>
void swap (char *x,char *y){
  char temp;
  temp=*x;
  *x=*y;
  *y=temp;
}
void permute(char *a,int i ,int n){
  int j;
  if (i==n)
    printf("%s\n",a);
  else{
    for(j=i ;j<=n;j++)
    {
      swap((a+i),(a+j));
      permute(a,i+1,n);
      swap((a+i),(a+j));
    }}}
int main(){
  char a[20];
  int n;
  scanf("%s",a);
  n=strlen(a);
  permute(a,0,n-1);
  getchar();
  return 0;
}

Possible paths

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
    int n;
    cin>>n;
    int graph[n][n];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>graph[i][j];
        }
    }
    int u,v,k;
    cin>>u>>v>>k;
    int count[n][n][k+1];
    for(int e=0;e<=k;e++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                count[i][j][e]=0;
                if(e==0&&i==j)
                    count[i][j][e]=1;
                if(e==1&&graph[i][j])
                    count[i][j][e]=1;
                if(e>1){
                    for(int a=0;a<n;a++){
                        if(graph[i][a])
                            count[i][j][e]+=count[a][j][e-1];
                    }
                }
            }
        }
    }
    cout<<count[u][v][k]<<endl;
}
return 0;
}

Knight Walk

#include <bits/stdc++.h>
using namespace std;
struct dist
{
    int x,y,d;
};
bool issafe(int x,int y,int n,int m)
{
    if(x>0 && x<=n && y>0 && y<=m)
    return true;
    return false;
}
void find(int s1,int s2,int d1,int d2,int n,int m)
{
    int visited[30][30];
    memset(visited,0,sizeof(visited));
    int i,j,x,y;
    struct dist p,z;
    p.x = s1;
    p.y = s2;
    p.d = 0;
    queue<struct dist>q;
    q.push(p);
    while(q.empty()==false)
    {
        p = q.front();
        //cout<<p.x<<" "<<p.y<<" ";
        q.pop();
        if(p.x==d1 && p.y==d2)
        {
            cout<<p.d<<endl;
            return;
        }
        int xMove[8] = {  2, 1, -1, -2, -2, -1,  1,  2 };
        int yMove[8] = {  1, 2,  2,  1, -1, -2, -2, -1 };
        for(i=0;i<8;i++)
        {
            x = p.x + xMove[i];
            y = p.y + yMove[i];
            if(issafe(x,y,n,m)==true && visited[x][y]==0)
            {
                z.x = x;
                z.y = y;
                z.d = p.d + 1;
                q.push(z);
                visited[x][y] = 1;
            }
        }
    }
    cout<<-1<<endl;
}
int main() {
    int t,i,j,n,m,s1,s2,d1,d2;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        cin>>s1>>s2>>d1>>d2;
        find(s1,s2,d1,d2,n,m);
    }
return 0;
}

SUPER TWO LETTER STRINGS

#include<bits/stdc++.h>
using namespace std;
int main()
{
   long int i,j,dp[11][10001];
    for(i=1;i<=10;i++)
    {
        dp[i][0]=1;
        dp[i][1]=1;
        for(j=2;j<=10000;j++)
        {
            if(j>i)
               {
                 dp[i][j]=(2*dp[i][j-1]-dp[i][j-i-1]+1000000007)%1000000007;
               }
            else
              {
                  dp[i][j]=(2*dp[i][j-1])%1000000007;
              }
        }
    }
    int t,n,p;
    cin>>t;
    while(t--)
    {
        cin>>n>>p;
        cout<<dp[p][n]<<endl;
    }
}

Rectangular Land

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;


/* Prewritten code begins */
#define REP(i,n)    for(int i=0; i<(n); ++i)
#define FOR(i,a,b)  for(int i=(a); i<=(b); ++i)
/* Prewritten code ends */

const int maxN = 505;
string s[maxN];
int range[maxN][maxN];
int main() {
 int R, C, lastC, res = 0;
 cin >> R >> C;
 REP(r,R) cin >> s[r];
 REP(c,C) range[0][c] = s[0][c] == '.' ? 0 : maxN;
 FOR(r,1,R-1) REP(c,C) if(s[r][c] == '.') range[r][c] = min(range[r-1][c], r); else range[r][c] = maxN;
 REP(r1,R) FOR(r2,r1+1,R-1) {
  lastC = -1;
  REP(c,C) {
   if(s[r1][c] == '.' && s[r2][c] == '.') {
    if(range[r2][c] <= r1) {
     if(lastC != -1) res = max(res, 2*(r2-r1-1)+2*(c-lastC+1));
     else lastC = c;
    }
   } else {
    lastC = -1;
   }
  }
 }
 if(res == 0) cout << "impossible" << endl;
 else cout << res << endl;
 return 0;
}