1012 – Guilty Prince

Problem Link

Hints: Run Bfs to check for land
First : store initial position of prince
then run from this position
highest only 4 move can be use (-1,0),(1,0),(0,-1),(0,1)
and print the count;

 

View Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long

ll n,m,co=1;
char s[21][21];
int vis[21][21];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

ll bfs(ll sx,ll sy)
{
vis[sx][sy]=1;
for(ll i=0;i<4;i++) { ll tx=sx+dx[i]; ll ty=sy+dy[i]; if( tx>=0 &&tx=0 &&ty>t;
while(t–)
{

cin>>n>>m;

for(int i=0;i<m;i++)
scanf(“%s”,&s[i]);
ll sx=0,sy=0;

memset(vis,0,sizeof vis);
int f=0;
for(ll i=0;i<m;i++)
{

for(ll j=0;j<n;j++)
{if(s[i][j]==’@’)
{
sx=i;
sy=j;// pair<int,int> can be used
f=1;
co=1; //for each testcase
break; }
}

if(f==1)
break;

}

ll d=0;

d=bfs(sx,sy);
cout<<“Case “<<cs++<<“: “<<d<<endl;
}
return 0;
}

 

Advertisements

300: Maya Calendar

Problem Link

#Solution

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<map>

using namespace std;
string day[20]={“pop”,”no”,”zip”,”zotz”,”tzec”,”xul”,”yoxkin”,”mol”,”chen”,”yax”,”zac”,”ceh”,”mac”,”kankin”,”muan”,”pax”,”koyab”,”cumhu”,”uayet”};
string month[20] = {“imix”, “ik”, “akbal”, “kan”, “chicchan”, “cimi”, “manik”, “lamat”, “muluk”, “ok”, “chuen”, “eb”, “ben”, “ix”, “mem”, “cib”, “caban”, “eznab”, “canac”, “ahau”};
int main()
{
int n,d,a;
string st;
long long x=0;
cin>>n;
cout<<n<<endl;
while(n–)

{
scanf(“%d.”,&d);
cin>>st>>a;

x=0;
map<string, int> day;

day[“pop”]=0;
day[“no”]=1;
day[“zip”]=2;
day[“zotz”]=3;
day[“tzec”]=4;
day[“xul”]=5;
day[“yoxkin”]=6;
day[“mol”]=7;
day[“chen”]=8;
day[“yax”]=9;
day[“zac”]=10;
day[“ceh”]=11;
day[“mac”]=12;
day[“kankin”]=13;
day[“muan”]=14;
day[“pax”]=15;
day[“koyab”]=16;
day[“cumhu”]=17;
day[“uayet”]=18;
int f=day[st];
x=a*365+d+20*f;
cout<<(x%13)+1<<‘ ‘<<month[x%20]<<‘ ‘<<x/260<<endl;

}

return 0;
}

USACO Open 2009 Silver :Cow Line

From BNUOJ

#Solution

#include<iostream>
#include<stdio.h>
#include<deque>
using namespace std;

int main()
{
std::ios::sync_with_stdio(false);
int t,co=0,n=-1;
cin>>t;deque<int>cows;
while(t–)
{
char a,s;
cin>>a;
if(a==’A’)
{
cin>>s;
co++;
if(s==’L’)
{
cows.push_front(co);
}
else if(s==’R’)
cows.push_back(co);
}
else{
cin>>s>>n;
if(s==’R’)
{
for(int i=0;i<n;i++)
cows.pop_back();
}
else
{

for(int i=0;i<n;i++)
cows.pop_front();
}
}
}
for(deque<int>::iterator it=cows.begin();it!=cows.end();it++)
printf(“%d\n”,*it);
}

1138 – Trailing Zeroes (III)

Problem Link

#Solution

#include<bits/stdc++.h>
using namespace std;
int co,m;
int zero(int n)
{
int q=5;
co=0;
int t=q;
while(n/t)         //while(n)
{                              //{

co+=n/t;      //{n/=5;
t*=q;        //co+=n;
}
return co;
}
int bs(int start,int end)
{
int mid=(start+end)>>1;
if(start>end)
return -1;

if(zero(mid)==m)
{
while(zero(mid)==m)
mid–;
return mid+1;

}

if(zero(mid)<m)
return bs(mid+1,end);
else if(zero(mid)>m)
return bs(start,mid-1);
}
int main()
{
int t,cs=1;
cin>>t;
while(t–)
{cin>>m;
int ans=bs(1,1e9);
printf(“Case %d: “,cs++);
if(ans==-1)
puts(“impossible”);
else
cout<<ans<<endl;
}

}

11730 – Number Transformation

Problem Link

Same Problem : LightOJ

#Solution

#include<bits/stdc++.h>
using namespace std;
#define N 1010
int prime[N];
void sieve(){
bool notprime[N]={true,true};
for(int i=2;i<=50;i++)
for(int j=2*i;j<=N;j+=i)
notprime[j]=true;

int c=0;

for(int i=2;i<N;i++)
if(!notprime[i]) prime[c++]=i;
}
int bfs(int s,int t)
{
if(s==t)
return 0;
int dis[N]={};
queue<int>q;
q.push(s);

while(!q.empty())
{

int u=q.front();
q.pop();
for(int j=0;prime[j]<u;j++)
{
if(!(u%prime[j]))
{ int v=u+prime[j];
if(dis[v]) continue;
if(v<t){
dis[v]=dis[u]+1;
q.push(v);
}

else if(v==t)
{
return dis[u]+1;
}
else break;

}
}
}
return -1;
}
int main()
{
sieve();
int s,t,cs=1;
while(cin>>s>>t &&s &&t)
{

cout<<“Case “<<cs++<<“: “<<bfs(s,t)<<endl;
}
return 0;
}

424 – Integer Inquiry

#Problem Link

Hints: BigInteger;

#Solution

import java.util.*;
import java.math.*;class Main
{public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
BigInteger n=new BigInteger(“0”);
while(sc.hasNext())
{
BigInteger sum=sc.nextBigInteger();
if(sum.equals(BigInteger.valueOf(0)))
break;
n=n.add(sum);
}
System.out.println(n);
}
}