- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<queue>
- #include<cstring>
- using namespace std;
- typedef pair<int,int> pii;
- #define pf printf
- #define sf scanf
- #define MAX 1000
- char grid[50][50];
- bool vis[50][50];
- queue <pii> Q;
- int r , c;
- int dx[] = {0, 0, -1, 1};
- int dy[] = {-1, 1, 0, 0};
- bool valid(int x,int y)
- {
- return x>=0 && y>=0 && x<r && y<c && (grid[x][y]=='.');
- }
- int bfs(int x, int y)
- {
- int ux, uy, vx, vy, cnt=0;
- memset(vis, 0, sizeof(vis));
- while(!Q.empty()) Q.pop(); //queue clear
- vis[x][y] = true;
- Q.push(pii(x,y));
- while(!Q.empty())
- {
- pii u = Q.front(); Q.pop();
- ux = u.first; uy = u.second;
- for(int i=0; i<4 ;i++)
- {
- vx = ux + dx[i];
- vy = uy + dy[i];
- if(!valid(vx, vy)) continue;
- if(vis[vx][vy]) continue;
- cnt++;
- vis[vx][vy] = true;
- Q.push(make_pair(vx, vy));
- }
- }
- return cnt;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int sr, sc, kase=0;
- int test, t;
- int i, j;
- sf("%d", &test);
- while(test--)
- {
- sf("%d%d", &c, &r);
- for(i=0; i<r; i++)
- sf("%s", grid[i]);
- for(i=0; i<r; i++)
- for(j=0; j<c; j++)
- if(grid[i][j]=='@')
- sr = i , sc = j, i = j = c+r;
- int ans = bfs(sr, sc)+1;
- pf("Case %d: %d\n", ++kase, ans);
- }
- return 0;
- }
Thursday, May 7, 2015
1012 - Guilty Prince (Light OJ)
493B - Vasya and Wrestling (Codeforces)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<vector> using namespace std; #define pf printf #define sf scanf #define loop(i, n) for(i=0; i<(n); i++) #define MAX 10000 typedef long long ll; vector<int> a, b; int main() { ll n, m, sum1, sum2, i, sz, j; int last, cnt , ans ; while(cin >> n) { ans =0; cnt=0; sum1 = sum2 = 0; a.clear(); b.clear(); loop(j,n) { cin >> m; if(j==(n-1)) last = m; if(m>0) { a.push_back(m); sum1 +=m; } else { m *=(-1); b.push_back(m); sum2 += m; } } sz=0; //cout << sum1 << " " << sum2 << endl; if(sum1>sum2) ans = 1; else if(sum2>sum1) ans = 2; else { if(a.size()> b.size()) sz = b.size(); else sz = a.size(); loop(i, sz) { if(a[i]>b[i]) { ans = 1;; cnt++; break; } else if(a[i]<b[i]) { ans = 2; cnt++; break; } } if(cnt==0) { if(last>0) ans = 1; else ans = 2; } } if(ans==1) cout << "first" << endl; else if(ans==2) cout << "second" << endl; } return 0;
}
496B - Secret Combination (Codeforces)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define pf printf #define sf scanf #define loop(i, n) for(i=0; i<(n); i++) #define MAX 100 int n; string s, s1, minS; bool min(string s1, string s2) { int i; int check = false; loop(i, n) { if(s1[i]==s2[i]) continue; else if(s1[i]>s2[i]) { check = true; break; } else { check = false; break; } } return check; } void addOne() { int i; loop(i, n) { if(s1[i]=='9') s1[i] = '0'; else s1[i]++; } } void rightShift() { s = s.substr(n-1,1)+ s.substr(0,n-1); } int main() { int i, j; cin >> n; cin >> s; minS = s; loop(i, n) { s1 = s; loop(j, 9) { addOne(); if(min(minS, s1)) minS = s1; } rightShift(); if(min(minS, s)) minS = s; } cout << minS << endl; return 0; }
499B - Lecture (Codeforces)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<map> using namespace std; #define pf printf #define sf scanf #define loop(i, n) for(i=0; i<(n); i++) #define MAX 100 map<string, string> mp; int main() { //freopen("in.txt","r",stdin); int n, m; string s1, s2; cin >> n >> m; while(m--) { cin >> s1 >> s2; if(s1.length()>s2.length()) mp[s1] = s2; else mp[s1] = s1; } while(n--) { cin >> s1; cout << mp[s1] << " "; } cout << endl; return 0; }
508B - Anton and currency you all know (Codeforces)
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define pf printf #define sf scanf #define loop(i, n) for(i=0; i<(n); i++) #define MAX 100 int main() { string s; int i, len; int index, temp, odd; int flag=0; cin >> s; len = s.length(); odd = s[len-1]-'0'; for(i=len-1; i>=0; i--) { temp = s[i]-'0'; if(temp%2==0) { flag++; if(flag==1) { index = i; } if(odd>temp) { index = i; } } } if(flag==0) cout <<"-1" << endl; else { swap(s[index], s[len-1]); cout << s << endl; } return 0; }
Friday, May 1, 2015
465A - inc ARG
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define sf scanf
#define pf printf
#define loop(i, n) for(i=0; i<(n) ; i++)
#define MAX 100
int main()
{
string s;
int n, i;
cin >> n;
cin >> s;
int c = 1;
int count = 0;
loop(i, n)
{
if(s[i]=='0' && c==1)
{
count++;
c = 0;
}
else if(s[i]=='1' && c==1)
{
count++;
c = 1;
}
else
break;
}
cout << count << endl;
return 0;
}
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define sf scanf
#define pf printf
#define loop(i, n) for(i=0; i<(n) ; i++)
#define MAX 100
int main()
{
string s;
int n, i;
cin >> n;
cin >> s;
int c = 1;
int count = 0;
loop(i, n)
{
if(s[i]=='0' && c==1)
{
count++;
c = 0;
}
else if(s[i]=='1' && c==1)
{
count++;
c = 1;
}
else
break;
}
cout << count << endl;
return 0;
}
Subscribe to:
Posts (Atom)
Compare equality of two string in C
#include <stdio.h> #include<string.h> int main() { char* country = "Bangladesh"; char* country2; ...
-
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespac...
-
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<ve...
-
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; #define pf printf ...