Tags » BF's

ONEZERO - Ones and zeros

http://www.spoj.com/problems/ONEZERO/

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;

/* Name of the class has to be "Main" only if the class is public. 348 more words
SPOJ Solutions

POJ 1426

10/16/2017
Problem: http://poj.org/problem?id=1426
Solution: BFS. Manage a queue, and for each time push current number + 0 or 1 in
the end all the way untill curr is a multiple of given n. 72 more words

BFS

POJ 3278

10/13/2017
Problem: http://poj.org/problem?id=3278
Solution: For every element x in the queue, add in x-1. x+1, x*2.


#include<iostream>
#include<queue>
using namespace std;

bool v;

bool inrange(int x) {
	return (x >= 0) && (x <= 100000); 
}

struct node{
	int t;
	int x;
	node(int tt, int xx) {
		t = tt;
		x = xx;
	}
};

int main() {
	int n, k;
	cin >> n >> k;
	queue<node> q;
	q.push(node(0, n));
	v[n] = true;
	while(!q.empty()) {
		node curr = q.front();
		q.pop();
		if (curr.x == k) {
			cout << curr.t;
			return 0;
		}
		if (inrange(curr.x - 1) && !v) {
			q.push(node(curr.t + 1, curr.x - 1));
			v = true;
		} 
		if (inrange(curr.x + 1) && !v) {
			q.push(node(curr.t + 1, curr.x + 1));
			v = true;
		}
		if (inrange(curr.x * 2) && !v) {
			q.push(node(curr.t + 1, curr.x * 2));
			v = true;
		}
	}
	return 0;
} 14 more words
BFS

POJ 2251 Dungeon Master

10/13/2017
Problem: http://poj.org/problem?id=2251
Solution: A plain bfs searching. Only changing 2D to 3D, but the usage is the strategy is the same.


#include<iostream>
#include<string.h>
#include<cstring>
#include<queue>
using namespace std;

int x, y, z;
int xs, ys, zs, xe, ye, ze;
int step = 0;
char map;
bool v;

int dx = {1, 0, 0, -1, 0, 0}; 
int dy = {0, 1, 0, 0, -1, 0}; 
int dz = {0, 0, 1, 0, 0, -1}; 

struct node{
	int time;
	int x, y, z;
	node(int tt, int xx, int yy, int zz) {
		time = tt;
		x = xx;
		y = yy;
		z = zz;
	}
};

bool inrange(int xx, int yy, int zz) {
	return (xx >= 0 && yy >= 0 && zz >= 0 && xx < x && yy < y && zz < z);
}

bool bfs() {
	queue<node> q;
	q.push(node(0, xs, ys, zs));
	v = true;
	while (!q.empty()) {
		node curr = q.front();
		q.pop();
		if (curr.x == xe && curr.y == ye && curr.z == ze) {
			step = curr.time;
			return true;
		} else {
			for (int i = 0; i < 6; i++) {
				int xx = curr.x + dx[i];
				int yy = curr.y + dy[i];
				int zz = curr.z + dz[i];
				if (inrange(xx, yy, zz) && (map == '.' || map == 'E') && !v) {
					q.push(node(curr.time + 1, xx, yy, zz));
					v = true;
				}
			}
		}
	}
	return false;
}

int main() {
	while (cin >> x >> y >> z) {
		if (x == 0 && y == 0 && z == 0) {
			return 0;
		}
		memset(map, 0, sizeof(map));
		memset(v, 0, sizeof(v));
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				cin >> map[i][j];
				for (int k = 0; k < z; k++) {
					if (map[i][j][k] == 'S') {
						xs = i;
						ys = j;
						zs = k;
					} else if (map[i][j][k] == 'E') {
						xe = i;
						ye = j;
						ze = k;
					}
				}
			}
		}
		if (bfs()) {
			cout << "Escaped in " << step << " minute(s).";
		} else {
			cout << "Trapped!";
		}
		cout << endl;
	}
	return 0;
} 15 more words
BFS

Number of Distinct Islands

问题在于如何存/translate an island so you can distinct them from each other.

我的代码跑过了所有700+个test case但是就是最后两个跑不过,而且test case太大不可能debug.最终拿了别人的pass的代码一个一个比较output才明白为什么我卡在最后两个test case了…

我的:

  • main method里面
    • boolean[][] visited和String set;
    • for loop through grids,碰到1且not visited就进入translate method, output一个string, 加入到set里面.
  • 91 more words

The Get Happy Tour 2018 Presents: Bowling For Soup!

Looking back to the late 00’s, fans of pop punk, punk rock and ska will fondly remember The Get Happy Tour; Bowling For Soup’s touring package that brought together bands like The Bloodhound Gang, Zebrahead and Army Of Freshmen, and of course Bowling For Soup themselves. 478 more words

News

Light OJ 1049 - One Way Roads [solved]

/******************************************
          Mobarak Hosen Shakil
        ICE, Islamic University
     ID: mhiceiuk(all), 29698(LOJ)
   E-mail: mhiceiuk @ (Gmail/Yahoo/FB)
 Blog: https://iuconvergent.wordpress.com
*******************************************/

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

#define mh                    main
#define IOS                   ios_base::sync_with_stdio(0);cin.tie(0)

#define F(i, a, b)            for(int i=a; i<b; i++)
#define B(i, b, a)            for(int i=b; i>=a; i--)

#define D                     double
#define LL                    long long int
#define ULL                   unsigned LL

#define Max(a,b)              ((a>b)?a:b)
#define Min(a,b)              ((a>b)?b:a)
#define _Max(a, b, c)         Max(a, Max(b, c))
#define _Min(a, b, c)         Min(a, Min(b, c))

#define all(a)                a.begin(), a.end()
#define srt(a, n)             sort(a, a+n)

#define chr                   getchar()
#define PI                    acos(-1)
#define sq(n)                 (n*n)
#define Bitcnt(a)             __builtin_popcountll(a)
#define mem(x, val)           memset(x, val, sizeof(x))
#define pb                    push_back
#define mp                    make_pair
#define sc                    scanf
#define pf                    printf
#define sz                    size
#define ft                    front
#define ps                    push
#define ff                    first
#define ss                    second
#define em                    empty

template<typename T>inline T Gcd(T a,T b){if(a<0)return Gcd(-a,b);if(b<0)return Gcd(a,-b);return (b==0)?a:Gcd(b,a%b);}
template<typename T>inline T Lcm(T a,T b) {if(a<0)return Lcm(-a,b);if(b<0)return Lcm(a,-b);return a*(b/Gcd(a,b));}
template<typename T>inline T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return sq(POW(B,P/2));}
template<typename T>inline T Bigmod(T base, T power, T MOD){T ret=1;while(power){if(power & 1)ret=(ret*base)%MOD;base=(base*base)%MOD;power>>=1;}return ret;}
template<typename T>inline T ModInverse(T number, T MOD){return Bigmod(number, MOD-2, MOD);}

///****************************************************************************///

typedef pair<int, int> pii;
typedef vector< pii > vii;
typedef vector<int> vi;
const int INF = (1<<30);

vi graph;
int cost;
bool chk;

void reset(int n)
{
    F(i, 1, n+1)
    {
        F(j, 1, n+1)
        {
            cost[i][j]=cost[j][i]=0;
        }
        chk[i]=1;
        graph[i].clear();
    }
}
int res=0;
void DFS(int u, int des)
{
    bool ok=false;
    if(chk[u]!=1) return;
    chk[u]=0;
    F(i, 0, graph[u].size())
    {
        int x=graph[u][i];
        ///cout<<res<<endl;
        if(chk[x]) {
            res+=cost[u][x];
            DFS(x, des);
            ok=true;
        }
    }
    if(!ok) res+=cost[u];
}

int mh()
{
    int tn, cn=0;
    sc("%d", &tn);
    while(tn--)
    {
        int n;
        sc("%d", &n);
        int x, y, c, total=0;
        reset(n);
        F(i, 1, n+1)
        {
            sc("%d%d%d", &x, &y, &c);
            graph[x].pb(y);
            graph[y].pb(x);
            cost[x][y]=0;cost[y][x]=c;
            total+=c;
        }
        DFS(1, 1);
       /// cout<<res<<endl;
        pf("Case %d: %d\n", ++cn, Min(res, total-res));
        res=0;
    }
    return 0;
}
Light OJ