F1
stringlengths 6
6
| F2
stringlengths 6
6
| text_1
stringlengths 20
200k
| text_2
stringlengths 20
200k
| label
int64 0
1
|
---|---|---|---|---|
C10132 | C10131 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
char dict[100];
char word1[] = {"zyeqejp mysljylc kd kxveddknmc re jsicpdrysirbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcdde kr kd eoya kw aej tysr re ujdr lkgc jv"};
char word2[] = {"qaozour language is impossible to understandthere are twenty six factorial possibilitiesso it is okay if you want to just give up"};
char inp1[256];
int main()
{
int i;
int n = strlen(word2);
for( i=0; i<n; i++)
{
if( word1[i]>='a' && word1[i]<='z')
dict[word1[i]-'a'] = word2[i];
}
int t=0;
cin >> t; cin.getline(inp1, 200);
for(int j=1; j<=t; j++ )
{
cout << "Case #" << j << ": ";
cin.getline(inp1,256);
for( i=0; i<strlen(inp1); i++)
{
if( inp1[i]>='a' && inp1[i]<='z')
cout << dict[inp1[i]-'a'];
else
cout << inp1[i];
}
cout << endl;
}
return 0;
}
| 0 |
C10132 | C10145 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define For(i, a, b) for (int i = (a); i != b; i++)
#define Rep(i,n) For(i,0,n)
#define debug(x) cout<<#x<<": "<<x<<endl
#define Pb push_back
#define Mp make_pair
template<class T>void show(T a,int n) {Rep(i,n)cout<<a[i]<<' ';cout<<endl;}
template<class T>void show(T a,int n,int m) {Rep(i,n){Rep(j,m)cout<<a[i][j]<<' ';cout<<endl;}}
int gcd(int a, int b) {
if (b == 0) {
return a;
} else if (a % b == 0) {
return b;
} else {
return gcd(b, a%b);
}
}
int main() {
int T;
char line[64], ch;
scanf("%d", &T);
gets(line);
//debug(line);
Rep(iT, T) {
printf("Case #%d: ", iT+1);
int H, W, D, ret = 0, x, y, a, b, d;
scanf("%d%d%d", &H, &W, &D);
gets(line);
//debug(line);
//while ((ch=getchar()) != '#') {}
Rep(i, H) {
gets(line);
//debug(line);
Rep(j,W) {
if (line[j] == 'X') {
b = i;
a = j;
}
}
}
d = D * 2;
x = (W - 2) * 2;
y = (H - 2) * 2;
a = a * 2 - 1;
b = b * 2 - 1;
//printf("%d %d %d %d %d ", x,y,a,b,d);
int d2 = d * d, m, n, dx, dy;
set<pair<int,int> > spi;
for (m = -(d/2/x+5); m < (d/2/x+5); m++) {
for (n = -(d/2/y+5); n < (d/2/y+5); n++) {
//if (m == 0 && n == 0) continue;
dx = 2*x*m, dy = 2*y*n;
if ((dx != 0 || dy != 0) && (dx*dx+dy*dy<=d2)) {
int gc = gcd(dx,dy);
if (gc < 0) gc = -gc;
spi.insert(make_pair(dx/gc,dy/gc));
}
dx = 2*x*m-2*a, dy = 2*y*n;
if ((dx != 0 || dy != 0) && (dx*dx+dy*dy<=d2)) {
int gc = gcd(dx,dy);
if (gc < 0) gc = -gc;
spi.insert(make_pair(dx/gc,dy/gc));
}
dx = 2*x*m, dy = 2*y*n-2*b;
if ((dx != 0 || dy != 0) && (dx*dx+dy*dy<=d2)) {
int gc = gcd(dx,dy);
if (gc < 0) gc = -gc;
spi.insert(make_pair(dx/gc,dy/gc));
}
dx = 2*x*m-2*a, dy = 2*y*n-2*b;
if ((dx != 0 || dy != 0) && (dx*dx+dy*dy<=d2)) {
int gc = gcd(dx,dy);
if (gc < 0) gc = -gc;
spi.insert(make_pair(dx/gc,dy/gc));
}
}
}
ret = spi.size();
printf("%d\n", ret);
}
return 0;
}
| 0 |
C10132 | C10147 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | /*****************************************************************************
*************************** Macros and Typedefs *****************************
*****************************************************************************/
// #pragma stacksize 1M twice
// #pragma comment(linked, "/STACK:16777216")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <time.h>
#include <vector>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi(n) fo(i,n)
#define fj(n) fo(j,n)
#define fk(n) fo(k,n)
#define fd(i,n) for(int i=(int)(n)-1; i>=0; --i)
#define fo(i,n) fr(i,0,n)
#define fr(i,a,b) for(int i=(int)a; i<(int)b; ++i)
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define srt(x) sort(all(x))
#define go(x,it) for(typeof((x).begin()) it=(x).begin(); it!=(x).end(); ++it)
#define PQ(t) priority_queue< t, vector<t>, greater<t> >
#define x first
#define y second
#define me (*this)
#define CLR(a,v) memset(a, v, sizeof(a))
typedef long long ll;
typedef long double ld;
typedef pair< int,int > ii;
typedef vector< ii > vii;
typedef vector< vii > vvii;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< ll > vll;
typedef vector< vll > vvll;
typedef vector< string > vs;
/*****************************************************************************
****************************** My Methods ***********************************
*****************************************************************************/
// my stuff
const int INF = 1000*1000*1000+7;
const double EPS = 1e-9;
int bit_count(int x){ return x==0 ? 0 : 1+bit_count(x&(x-1)); }
inline int low_bit(int x){ return x&-x; } // 0011 0100 return 0000 0100
inline int sign(double x){ return x<-EPS ? -1 : x>EPS ? 1 : 0; }
inline int sign(int x){ return (x>0)-(x<0); }
/*****************************************************************************
**************************** Scanner Methods ********************************
*****************************************************************************/
inline int getInt(){
int a;
return scanf("%d", &a) ? a : (fprintf(stderr, "trying to read\n"),-1);
}
inline double getDouble(){
double a;
return scanf("%lf", &a) ? a : (fprintf(stderr, "trying to read\n"),-1.0);
}
inline double myRand(){
return ((double)rand()/RAND_MAX) + ((double)rand()/RAND_MAX/RAND_MAX);
}
/*
* // does not get the new line
* getline(cin, s);
*
* srt(a); a.resize(unique(all(a))-a.begin());
*
* // remember, only do this for 0 and -1, INF etc may not work
* memset(dp, 0, sizeof(dp));
*
* struct comp_1{
* bool operator()(const ii &a, const ii &b) const{
* return a.x!=b.x ? a.x<b.x : a.y<b.y;
* }
* };
*
* sprintf(buf, "%s%d%s%d%s\n", "Hell", 0, " W", 0, "rld!");
*
* (map<int,int>::iterator it = cache.begin(); it!=cache.end(); ++it)
*/
const int BUF_SIZE = 1001*1000;
char buf[BUF_SIZE];
inline string getToken(){
return scanf("%s", buf) ? buf : (fprintf(stderr, "trying to read\n"),"");
}
// does not return the new line
inline string getLine(){
string ret;
getline(cin, ret);
// getline(cin, ret, ':'); // to use ':' as the delimiter
return ret;
}
/*****************************************************************************
************************* Problem Specific Code *****************************
*****************************************************************************/
// END_CUT
const double PI = 2*acos(0.0);
int m, n, d, si, sj;
char board[35][35];
int doit(){
si = 2*si-1;
sj = 2*sj-1;
m = 2*(m-2);
n = 2*(n-2);
d = 2*d;
vd thetas;
int ret=0;
fr(i,0,101) fr(j,0,101){
double mi = 2*((i+1)/2)*m + ( (i&1) ? -si : si ) - 50*m;
double mj = 2*((j+1)/2)*n + ( (j&1) ? -sj : sj ) - 50*n;
if(sqr(mi-si)+sqr(mj-sj) < sqr(d)+1e-6 && sqr(mi-si)+sqr(mj-sj) > 1e-6)
thetas.pb(atan2(mi-si, mj-sj));
}
srt(thetas);
/*
if(m==2 && n==2 && d==4){
fi(sz(thetas))
if(i==0 || thetas[i] > thetas[i-1]+EPS)
fprintf(stderr, "%lf ", thetas[i]);
fprintf(stderr, "\n");
}
*/
// first shot is okay, if not like the last shot
if(!thetas.empty() && thetas[0] > thetas.back()-2*PI+EPS)
ret=1;
// every other shot must make sure it is not identical to someone
fr(i,1,sz(thetas))
if(thetas[i] > thetas[i-1]+EPS)
++ret;
/*
fprintf(stderr, "si=%d, sj=%d, m=%d, n=%d, d=%d\n", si, sj, m, n, d);
// get all possible shots in order
fr(nu,-50,51) fr(nl,-50,51) fr(du,-1,2) fr(dl,-1,2){
// if shot is within distance
double ddu = 4*sqr(nu*si + (nu+du)*(m-si));
double ddl = 4*sqr(nl*sj + (nl+dl)*(n-sj));
// try all 4 quadrants, don't worry about counting double
if(ddu+ddl < sqr(d)+1e-6 && ddu+ddl > EPS){
if(du<=0 && dl>=0) thetas.pb(atan2(sqrt(ddu), sqrt(ddl)));
if(du<=0 && dl<=0) thetas.pb(atan2(sqrt(ddu), -sqrt(ddl)));
if(du>=0 && dl>=0) thetas.pb(atan2(-sqrt(ddu), sqrt(ddl)));
if(du>=0 && dl<=0) thetas.pb(atan2(-sqrt(ddu), -sqrt(ddl)));
}
}
srt(thetas);
if(m==4 && n==2 && d==16){
fi(sz(thetas)) if(i==0 || thetas[i]>thetas[i-1]+EPS)
fprintf(stderr, "%lf ", thetas[i]);
fprintf(stderr, "\n");
}
// first shot is okay, if not like the last shot
if(!thetas.empty() && thetas[0] > thetas.back()-2*PI+EPS)
ret=1;
// every other shot must make sure it is not identical to someone
fr(i,1,sz(thetas))
if(thetas[i] > thetas[i-1]+EPS)
++ret;
*/
return ret;
}
void myCode(){
int ttt=getInt();
fo(tt,ttt){
m=getInt(); n=getInt(), d=getInt();
fi(m){
string s=getToken();
fj(n){
board[i][j] = s[j];
if(board[i][j]=='X'){ si=i; sj=j; }
}
}
printf("Case #%d: %d\n", tt+1, doit());
}
}
int main() {
srand(time(NULL));
myCode();
return 0;
}
| 0 |
C10132 | C10005 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #pragma once
// -- libs includes
#include "core/types.h"
// ================================================================================================
// Class for managing a sieve for more quickly searching for prime numbers
// ================================================================================================
class CSieve {
protected:
nuint sievesize;
nuint offsetsize;
nuint* offsets;
public:
CSieve();
~CSieve();
// -- set up the sieve with the given primes
void Prepare(nuint* primes, nuint count);
// -- get an initial offset to move the given number onto a value we can iterate on
// -- also return the offset index to start using when iterating
nuint GetInitialOffset(nuint v, nuint& index);
// -- accessors
nuint NumOffsets();
nuint Offset(nuint i);
};
// ================================================================================================
// Constructor
// ================================================================================================
inline CSieve::CSieve() : sievesize(0), offsets(NULL) {
}
// ================================================================================================
// Accessors
// ================================================================================================
inline nuint CSieve::NumOffsets() {
return offsetsize;
}
inline nuint CSieve::Offset(nuint i) {
return offsets[i];
}
| 0 |
C10132 | C10022 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #pragma once
// -- libs includes
#include "core/types.h"
// ================================================================================================
// A class for taking high precision timings
// ================================================================================================
class CTimer {
protected:
uint64 start;
uint64 stop;
uint64 freq;
uint64 mode;
public:
// -- constructor (automatically starts)
CTimer();
// -- explicit start / stop
void Start();
void Stop();
// -- accessors to timing information
nreal GetSeconds() const;
nuint GetMicro() const;
// -- direct accessors
nuint GetStart() const;
nuint GetStop() const;
nuint GetCycles() const;
nuint GetFrequency() const;
};
// ================================================================================================
// Accessors
// ================================================================================================
inline nreal CTimer::GetSeconds() const {
return (stop - start) / nreal(freq);
}
inline nuint CTimer::GetMicro() const {
real64 conv = 1000000.0 / real64(freq);
return nuint((stop - start) * conv);
}
inline nuint CTimer::GetStart() const {
return start;
}
inline nuint CTimer::GetStop() const {
return stop;
}
inline nuint CTimer::GetCycles() const {
return stop - start;
}
inline nuint CTimer::GetFrequency() const {
return freq;
}
| 0 |
C10132 | C10104 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <stdio.h>
#include <memory.h>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, D;
#define MAX 3010
int SX, SY;
bool or[MAX][MAX];
bool original[MAX][MAX];
bool matr[MAX][MAX];
int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a % b);
}
int sign(int a)
{
return a == 0 ? 0 : a < 0 ? -1 : 1;
}
int change[16][2] =
{
{1,1},
{0,0},
{0,0},
{1,-1},
{0,0},
{1,1},
{-1,1},
{-1, -1},
{0,0},
{-1,1},
{1,1},
{-1,-1},
{1,-1},
{-1,-1},
{-1,-1},
{0,0}
};
bool f(int h, int w)
{
long long hm = abs(w);
long long wm = abs(h);
if (hm == 0)
hm = 1;
if (wm == 0)
wm = 1;
int x = SX * hm;
int y = SY * wm;
int tx = x;
int ty = y;
int dirs[4][2] = {{-1, -1}, {0, -1}, {0, 0}, {-1, 0}};
h = sign(h);
w = sign(w);
int hh = abs(h);
int ww = abs(w);
long long cnt = 0;
long long qq = (ww * hm * hm + hh * wm * wm);
long long zz = hm * hm * wm * wm * D * D;
while (true)
{
x = x + h;
y = y + w;
cnt++;
if (cnt * cnt * qq > zz)
break;
if (x == tx && y == ty)
return true;
int mask = 0;
for (int d = 0; d < 4; d++)
{
mask = mask << 1;
int xx = x + dirs[d][0];
int yy = y + dirs[d][1];
mask = mask + (original[xx / hm][yy / wm] ? 1 : 0);
}
h = h * change[mask][0];
w = w * change[mask][1];
if (x == 0 && y == 0)
return false;
}
return false;
}
int main()
{
freopen("d.in", "r", stdin);
freopen("d.out", "w", stdout);
int T;
scanf("%d", &T);
for (int t = 0; t < T; t++)
{
scanf("%d%d%d", &N, &M, &D);
for (int i = 0; i < N; i++)
{
char s[100];
scanf("%s", s);
for (int j = 0; j < M; j++)
{
if (s[j] == 'X')
{
SX = i;
SY = j;
}
or[i][j] = s[j] == '#';
}
}
N *= 2;
M *= 2;
D *= 2;
SX = SX * 2 + 1;
SY = SY * 2 + 1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
original[i][j] = or[i/2][j/2];
int res = 0;
if (f(0, 1))
res++;
if (f(0, -1))
res++;
if (f(1, 0))
res++;
if (f(-1, 0))
res++;
for (int i = 1; i <= D; i++)
for (int j = 1; j <= D; j++)
if (i*i + j*j <= D*D)
if (gcd(i, j) == 1)
{
if (f(i, j))
res++;
if (f(i, -j))
res++;
if (f(-i, j))
res++;
if (f(-i, -j))
res++;
}
res = res + 0;
printf("Case #%d: %d\n", t+1, res);
}
return 0;
} | 0 |
C10132 | C10156 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<ctype.h>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<complex>
#define EPS (1e-10)
#define PI (3.141592653589793238)
#define MP make_pair
typedef long long ll;
using namespace std;
int myhypot(int a,int b){
return a*a+b*b;
}
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int sign(int x){
if(x>0)return 1;
if(x==0)return 0;
if(x<0)return -1;
}
int main(void){
int casenum;
scanf("%d",&casenum);
for(int casecnt=1;casecnt<=casenum;casecnt++){
int xx,yy,d;
int sx,sy;
scanf("%d %d %d",&yy,&xx,&d);
for(int j=0;j<yy;j++){
char ch[100];
scanf("%s",ch);
for(int i=0;i<xx;i++){
if(ch[i]=='X'){
sx=i-1;
sy=j-1;
}
}
}
xx-=2;
yy-=2;
vector<pair<int,int> > pos;
for(int i=-60;i<=60;i++){
for(int j=-60;j<=60;j++){
if(i==0 && j==0)continue;
int tx=sx,ty=sy;
if(abs(i)%2==1)tx=xx-1-sx;
if(abs(j)%2==1)ty=yy-1-sy;
tx+=i*xx;
ty+=j*yy;
if(myhypot(tx-sx,ty-sy)<=d*d){
pos.push_back(MP(tx-sx,ty-sy));
}
}
}
vector<bool> dame(pos.size());
for(int i=0;i<pos.size();i++){
for(int j=i+1;j<pos.size();j++){
int x1=pos[i].first;
int y1=pos[i].second;
int x2=pos[j].first;
int y2=pos[j].second;
if(sign(x1)!=sign(x2) || sign(y1)!=sign(y2))continue;
x1=abs(x1);
y1=abs(y1);
x2=abs(x2);
y2=abs(y2);
int g1=gcd(x1,y1);
int g2=gcd(x2,y2);
x1/=g1;
y1/=g1;
x2/=g2;
y2/=g2;
if(x1==x2 && y1==y2)dame[j]=true;
}
}
int ans=0;
for(int i=0;i<pos.size();i++){
if(!dame[i])ans++;
}
printf("Case #%d: %d\n",casecnt,ans);
fflush(stdout);
}
return 0;
}
| 0 |
C10132 | C10327 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max(x, y) ((x)>(y)?(x):(y))
#define min(x, y) ((x)<(y)?(x):(y))
#define abs(x) ((x)<0?-(x):(x))
char buf[1024];
typedef long long int64;
int R, C, D;
int pnt_len = 0;
typedef struct pnt_s {
int r;
int c;
} pnt_t;
pnt_t pnt[10000];
int add_pnt( int r, int c)
{
if ( r*r+c*c <= D*D )
{
pnt[pnt_len].r = r;
pnt[pnt_len].c = c;
pnt_len++;
}
return 0;
}
int compare( const void *a, const void *b )
{
pnt_t *a1 = (pnt_t *)a;
pnt_t *b1 = (pnt_t *)b;
return (a1->r)*(b1->c) - (b1->r)*(a1->c);
}
int F(int col, int row)
{
pnt_len = 0;
//
// 1. determine distance in D
for (int m=0; 2*m*R<=D; m++)
{
int r = 2*m*R;
for (int k=0; 2*k*C<=D; k++)
{
int c = 2*k*C;
int r1, c1;
r1 = r + row*2 + 1;
c1 = c + (C-1-col)*2 + 1;
add_pnt(r1, c1);
r1 = r + row*2 + 1;
c1 = c + 2*C;
add_pnt(r1, c1);
r1 = r + 2*R;
c1 = c + (C-1-col)*2 + 1;
add_pnt(r1, c1);
r1 = r + 2*R;
c1 = c + 2*C;
add_pnt(r1, c1);
}
}
//
// 2. Remove same angle
// sort by tangent
qsort( pnt, pnt_len, sizeof(pnt_t), compare);
// count dup
int dup_count = 0;
float last_tng = 0.0;
int last_r = 0;
int last_c = 100;
for (int j=0; j<pnt_len; j++)
{
if ( last_r*pnt[j].c == pnt[j].r*last_c )
dup_count ++;
last_r = pnt[j].r;
last_c = pnt[j].c;
}
return pnt_len - dup_count;
}
int main()
{
gets(buf);
int ncase = atoi(buf);
for(int i=1; i<=ncase; i++)
{
int H, W;
scanf("%d %d %d", &H, &W, &D);
R = H-2;
C = W-2;
int workcnt = 0;
int col = 0;
int row = 0;
for (int k=0; k<H; k++)
{
//gets(buf);
scanf("%s", buf);
char* ret = strchr(buf, 'X');
if ( ret != NULL )
{
col = ret-buf-1;
row = k-1;
}
}
//
// A. up, down, left, right
//
if ( row*2+1 <= D )
workcnt++;
if ( (R-1-row)*2+1<= D )
workcnt++;
if ( col*2+1 <= D )
workcnt++;
if ( (C-1-col)*2+1 <= D )
workcnt++;
//
// B diagonal
//
workcnt += F(col, row);
workcnt += F(col, R-1-row);
workcnt += F(C-1-col, R-1-row);
workcnt += F(C-1-col, row);
printf("Case #%d: ", i);
printf("%d", workcnt);
printf("\n");
}
return 0;
}
| 0 |
C10132 | C10085 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <sstream>
#include <cstddef>
#include <algorithm>
#include <utility>
#include <iterator>
#include <numeric>
#include <list>
#include <complex>
#include <cstdio>
#include <climits>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef complex<double> pnt;
typedef pair<int, int> pii;
#define RA(x) (x).begin(), (x).end()
#define FE(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define SZ(x) ((int) (x).size())
template<class T>
void splitstr(const string &s, vector<T> &out)
{
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
static void redirect(int argc, const char **argv)
{
if (argc > 1)
{
int fd = open(argv[1], O_RDONLY);
if (fd == -1) { perror(argv[1]); exit(1); }
if (-1 == dup2(fd, 0)) { perror(argv[1]); exit(1); }
if (-1 == close(fd)) { perror(argv[1]); exit(1); }
}
if (argc > 2)
{
int fd = open(argv[2], O_WRONLY | O_CREAT, 0666);
if (fd == -1) { perror(argv[2]); exit(1); }
if (-1 == dup2(fd, 1)) { perror(argv[2]); exit(1); }
if (-1 == close(fd)) { perror(argv[2]); exit(1); }
}
}
static int roundup(int x, int m)
{
assert(m > 0);
return (x + m - 1) / m * m;
}
static int gcd(int a, int b ) { return b ? gcd(b, a % b) : a; }
static bool get(const vs &grid, int r, int c)
{
assert(r >= 0 && r < SZ(grid) && c >= 0 && c < SZ(grid[r]));
return grid[r][c] == '#';
}
static const int deltar[4] = {-1, 0, 1, 0};
static const int deltac[4] = {0, -1, 0, 1};
int main(int argc, const char **argv)
{
redirect(argc, argv);
int cases;
cin >> cases;
for (int cas = 0; cas < cases; cas++)
{
int H, W, D;
cin >> H >> W >> D;
int sr = -1, sc = -1;
vs grid(H);
for (int i = 0; i < H; i++)
{
cin >> grid[i];
for (int j = 0; j < W; j++)
if (grid[i][j] == 'X')
{
grid[i][j] = '.';
sr = i;
sc = j;
}
}
assert(sr != -1);
set<pair<int, int> > good;
int ans = 0;
for (int dir = 0; dir < 4; dir++)
{
int r = sr;
int c = sc;
int dist = 0;
while (grid[r][c] != '#')
{
r += deltar[dir];
c += deltac[dir];
dist++;
}
if (dist * 2 - 1 <= D)
ans++;
}
sr = 2 * sr + 1;
sc = 2 * sc + 1;
D *= 2;
for (int tr = sr - D; tr <= sr + D; tr++)
for (int tc = sc - D; tc <= sc + D; tc++)
{
int dr = tr - sr;
int dc = tc - sc;
int adr = abs(dr);
int adc = abs(dc);
int scale = adr * adc;
if (scale > 0 && adr * adr + adc * adc <= D * D)
{
int beginr = sr * scale;
int beginc = sc * scale;
int endr = beginr;
int endc = beginc;
int stepr = dr;
int stepc = dc;
int t = 0;
int T = scale;
while (true)
{
int nextr = roundup(t + adr + 1, 2 * adr) - adr;
int nextc = roundup(t + adc + 1, 2 * adc) - adc;
t = min(nextr, nextc);
if (t >= T)
break;
int r = beginr + t * stepr;
int c = beginc + t * stepc;
bool to = get(grid, (r + stepr) / (2 * scale), (c + stepc) / (2 * scale));
if (to)
{
bool hflip = false, vflip = false, kill = false;
if (nextr < nextc) // vertical edge
hflip = true;
else if (nextr > nextc) // horizontal edge
vflip = true;
else // corner
{
hflip = get(grid, (r - stepr) / (2 * scale), (c + stepc) / (2 * scale));
vflip = get(grid, (r + stepr) / (2 * scale), (c - stepc) / (2 * scale));
if (!hflip && !vflip)
kill = true;
}
if (kill)
break;
if (hflip)
{
beginc = 2 * c - beginc;
stepc = -stepc;
}
if (vflip)
{
beginr = 2 * r - beginr;
stepr = -stepr;
}
}
}
if (t >= T)
{
int r = beginr + T * stepr;
int c = beginc + T * stepc;
if (r == endr && c == endc)
{
int g = gcd(adr, adc);
good.insert(pii(dr / g, dc / g));
}
}
}
}
ans += good.size();
printf("Case #%d: %d\n", cas + 1, ans);
}
return 0;
}
| 0 |
C10132 | C10084 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define MAXN 105
using namespace std;
struct node
{
int x, y;
bool operator<(const node& p) const
{
return ((x * p.y - y * p.x) < 0);
}
};
int GetVec (struct node a, struct node b)
{
return a.x * b.y - a.y * b.x;
}
double GetDist (int sx, int sy, int fx, int fy)
{
return sqrt((long double)((fx - sx) * (fx - sx)) + (long double)((fy - sy) * (fy - sy)));
}
double GetDistD (double vx, double vy)
{
return sqrt(vx * vx + vy * vy);
}
int main ()
{
int T, iT;
scanf("%d",&T);
static char a[MAXN][MAXN];
static struct node srt[MAXN*MAXN];
int srtc;
static struct node d[MAXN*MAXN];
int dc;
for (iT = 0; iT < T; iT++)
{
int res = 0;
int N, M, D;
scanf("%d %d %d\n",&N,&M,&D);
int i, j;
for (i = 0; i < N; i++) scanf("%s\n",a[i]);
int sx, sy, fx, fy;
sx = 0; sy = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
if (a[i][j] == 'X')
{
sx = i;
sy = j;
}
}
}
dc = 2;
d[0].x = 0;
d[0].y = 1;
d[1].x = 0;
d[1].y = -1;
srtc = 0;
for (fx = sx + 1; fx <= sx + D; fx++)
{
for (fy = sy - D; fy <= sy + D; fy++)
{
if (((fx != sx) || (fy != sy)) && ((GetDist(sx,sy,fx,fy) - 1E-9) <= (long double)(D)))
{
srt[srtc].x = fx - sx;
srt[srtc].y = fy - sy;
srtc++;
}
}
}
sort(&(srt[0]),&(srt[srtc]));
for (i = 0; i < srtc; i++)
{
//printf("%d %d\n",srt[i].x,srt[i].y);
if ((i == 0) || (GetVec(srt[i], srt[i-1]) != 0))
{
d[dc] = srt[i];
dc++;
}
}
srtc = 0;
for (fx = sx - D; fx <= sx - 1; fx++)
{
for (fy = sy - D; fy <= sy + D; fy++)
{
if (((fx != sx) || (fy != sy)) && ((GetDist(sx,sy,fx,fy) - 1E-9) <= (long double)(D)))
{
srt[srtc].x = fx - sx;
srt[srtc].y = fy - sy;
srtc++;
}
}
}
sort(&(srt[0]),&(srt[srtc]));
for (i = 0; i < srtc; i++)
{
if ((i == 0) || (GetVec(srt[i], srt[i-1]) != 0))
{
d[dc] = srt[i];
dc++;
}
}
for (i = 0; i < dc; i++)
{
long double x, y, finx, finy;
long double vx, vy;
vx = (long double)(d[i].x); vy = (long double)(d[i].y);
//printf("%.6lf %.6lf\n",(double)vx,(double)vy);
x = (long double)(sx) - 0.5;
y = (long double)(sy) - 0.5;
finx = x; finy = y;
long double T = 0.0;
while ((T - 1E-9) <= (long double)(D))
{
if ((T > 0.5) && (fabs(finx - x) <= 1E-9) && (fabs(finy - y) <= 1E-9))
{
res++;
//printf("%d %d @ (%.3lf; %.3lf) for %.3lf\n",d[i].x,d[i].y,(double)(x),(double)(y),(double)(T));
break;
}
long double ta, tb, tres;
ta = 1E10; tb = 1E10;
long double bx, by;
if (vx > 1E-9)
{
bx = ceil(x * 2.0 + 1E-7) / 2.0;
ta = (bx - x) / (double)(vx);
}
else if (vx < -1E-9)
{
bx = floor(x * 2.0 - 1E-7) / 2.0;
ta = (bx - x) / (double)(vx);
}
if (vy > 1E-9)
{
by = ceil(y * 2.0 + 1E-7) / 2.0;
tb = (by - y) / (double)(vy);
}
else if (vy < -1E-9)
{
by = floor(y * 2.0 - 1E-7) / 2.0;
tb = (by - y) / (double)(vy);
}
if (ta < tb) tres = ta;
else tres = tb;
//printf("(%.6lf; %.6lf) -> %.6lf\n",(double)(x), (double)(y), (double)(tres));
x += vx * tres;
y += vy * tres;
T += (tres * GetDistD(vx, vy));
//if ((d[i].x == 2) && (d[i].y == 5)) printf("(%.6lf; %.6lf)\n",(double)x,(double)y);
//printf("(%.6lf; %.6lf)\n",(double)x,(double)y);
bool OKx, OKy;
int X, Y;
OKx = ((fabs(x - floor(x + 1E-7))) <= 1E-9);
OKy = ((fabs(y - floor(y + 1E-7))) <= 1E-9);
//printf("%d %d\n",(int)OKx,(int)OKy);
bool decided = false;
int cnt;
//if ((d[i].x == 2) && (d[i].y == 5)) printf(">> %d %d\n",(int)(OKx),(int)(OKy));
if ((OKx) && (OKy))
{
X = (int)(x + 0.25);
Y = (int)(y + 0.25);
cnt = 0;
if (a[X][Y] == '#') cnt++;
if (a[X][Y+1] == '#') cnt++;
if (a[X+1][Y] == '#') cnt++;
if (a[X+1][Y+1] == '#') cnt++;
struct node to;
if ((vx >= 0.0) && (vy >= 0.0))
{
//from.x = X; from.y = Y;
to.x = X+1; to.y = Y+1;
}
else if ((vx >= 0.0) && (vy < 0.0))
{
//from.x = X; from.y = Y+1;
to.x = X+1; to.y = Y;
}
else if ((vx < 0.0) && (vy >= 0.0))
{
to.x = X; to.y = Y+1;
//from.x = X+1; from.y = Y;
}
else
{
to.x = X; to.y = Y;
//from.x = X+1; from.y = Y+1;
}
if (a[to.x][to.y] == '#')
{
if (cnt == 1) break;
else if (cnt == 3)
{
vx *= -1.0;
vy *= -1.0;
decided = true;
}
}
else
{
decided = true;
}
}
if (!decided)
{
if (OKx)
{
X = (int)(x + 0.25);
if (vx >= 0.0) X++;
if (vy >= 0.0) Y = (int)(ceil(y - 1E-7) + 1E-7);
else Y = (int)(ceil(y + 1E-7) + 1E-7);
if (a[X][Y] == '#')
{
vx *= -1.0;
decided = true;
}
}
if ((!decided) && (OKy))
{
Y = (int)(y + 0.25);
if (vy >= 0.0) Y++;
if (vx >= 0.0) X = (int)(ceil(x - 1E-7) + 1E-7);
else X = (int)(ceil(x + 1E-7) + 1E-7);
//if ((d[i].x == 2) && (d[i].y == 5)) printf("%d %d\n",X,Y);
if (a[X][Y] == '#') vy *= -1.0;
}
}
}
//printf("%d %d @ (%.3lf; %.3lf) for %.3lf\n",d[i].x,d[i].y,(double)(x),(double)(y),(double)(T));
}
printf("Case #%d: %d\n",iT+1,res);
}
}
| 0 |
C10132 | C10032 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include "stdafx.h"
| 0 |
C10132 | C10301 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <sstream>
#include <Windows.h>
#include <boost/rational.hpp>
#include <boost/tokenizer.hpp>
using namespace std;
class FileReader : public ifstream
{
public:
FileReader( const string& filename ) { open( filename.c_str(), ios_base::in ); }
int readInt() { int x; *this >> x; return x; }
vector<int> readInts( int n ) { vector<int> v(n); for ( int i = 0; i < n; i++ ) v[i] = readInt(); return v; }
string readLine() { char buf[20000]; getline( buf, sizeof(buf) ); return buf; }
//vector<string> readLines( int n ) { vector<string> v; for ( int i = 0; i < n; i++ ) v.push_back( readLine() ); return v; }
string readString() { string x; *this >> x; return x; }
vector<string> readStrings( int n ) { vector<string> v; for ( int i = 0; i < n; i++ ) v.push_back( readString() ); return v; }
__int64 readInt64() { __int64 x; *this >> x; return x; }
};
//vector<string> parse( const string& s, const string& delimeters = " ,\t" )
//{
// vector<string> ret;
// boost::tokenizer<boost::char_separator<char>> tokens( s, boost::char_separator<char>( delimeters.c_str() ) );
// copy( tokens.begin(), tokens.end(), back_inserter( ret ) );
// return ret;
//}
class FileWriter : public ofstream
{
public:
FileWriter( const string& filename ) { open( filename.c_str(), ios_base::out ); }
//int writeInt() { int x; *this << x; return x; }
//int writeString( const string& str ) { *this << str; }
};
typedef boost::rational<int> rat;
rat up( const rat& x ) { return rat( x.numerator()/x.denominator()+1 ); }
rat down( const rat& x ) { return rat( (x.numerator()-1)/x.denominator() ); }
rat abs( const rat& x ) { return rat( abs( x.numerator() ), x.denominator() ); }
rat isInt( const rat& x ){ return x.denominator() == 1; }
double toDouble( const rat& x ){ return (double)x.numerator()/x.denominator(); }
class XY
{
public:
XY() : x(0), y(0) {}
XY( const rat& x, const rat& y ) : x(x), y(y) {}
bool operator==( const XY& p ) const { return x==p.x && y==p.y; }
bool operator!=( const XY& p ) const { return !(*this == p); }
XY operator-( const XY& p ) const { return XY( x-p.x, y-p.y ); }
XY operator*( const rat& m ) const { return XY( x*m, y*m ); }
rat manhattanDist() const { return abs( x ) + abs( y ); }
rat len2() const { return x*x + y*y; }
public:
rat x, y;
};
__int64 gcd( __int64 a, __int64 b ) { return a ? gcd( b%a, a ) : b; }
XY g_start;
XY nextBoundary( const XY& pos, const XY& dir )
{
if ( pos != g_start )
{
// check if we hit starting position
XY d = g_start-pos;
rat ratio = d.y != 0 ? dir.y / d.y : dir.x / d.x;
if ( d * ratio == dir )
{
if ( ratio > 0 && d.manhattanDist() <= 1 )
return g_start;
}
}
XY bound0;
XY bound1;
if ( dir.x != 0 )
{
rat newX = dir.x > 0 ? up( pos.x ) : down( pos.x );
rat newY = pos.y + (newX-pos.x)*(dir.y/dir.x);
bound0 = XY( newX, newY );
}
if ( dir.y != 0 )
{
rat newY = dir.y > 0 ? up( pos.y ) : down( pos.y );
rat newX = pos.x + (newY-pos.y)*(dir.x/dir.y);
bound1 = XY( newX, newY );
}
if ( bound0 == XY() ) return bound1;
if ( bound1 == XY() ) return bound0;
return ( bound0 - pos ).manhattanDist() <= ( bound1 - pos ).manhattanDist() ? bound0 : bound1;
}
int doit( FileReader& fin )
{
int SY = fin.readInt();
int SX = fin.readInt();
int D = fin.readInt();
vector<string> m = fin.readStrings( SY );
for ( int y = 0; y < SY; y++ )
for ( int x = 0; x < SX; x++ )
if ( m[y][x] == 'X' )
g_start = XY( x + rat(1,2), y + rat(1,2) );
int ret = 0;
for ( int dirX = -D; dirX <= D; dirX++ )
for ( int dirY = -D; dirY <= D; dirY++ ) if ( gcd( abs(dirX), abs(dirY) ) == 1 )
{
if ( dirX*dirX + dirY*dirY > D*D ) continue;
rat totalDist = 0;
XY dir = XY( dirX, dirY );
XY pos = g_start;
rat distLimit = D*D * dir.manhattanDist()*dir.manhattanDist() / dir.len2();
while ( 1 )
{
XY newPos = nextBoundary( pos, dir );
totalDist += ( newPos - pos ).manhattanDist();
double dddd = toDouble(totalDist) * sqrt(toDouble(dir.len2())) / toDouble(dir.manhattanDist());
if ( totalDist*totalDist > distLimit )
break;
if ( newPos == g_start )
{
ret++;
break;
}
int newX = newPos.x.numerator() - (dir.x < 0);
int newY = newPos.y.numerator() - (dir.y < 0);
int oldX = newPos.x.numerator() - (dir.x > 0);
int oldY = newPos.y.numerator() - (dir.y > 0);
if ( !isInt( newPos.x ) ) { newX = oldX = newPos.x.numerator() / newPos.x.denominator(); }
if ( !isInt( newPos.y ) ) { newY = oldY = newPos.y.numerator() / newPos.y.denominator(); }
//if ( isInt( newPos.x ) && isInt( newPos.y ) )
//{
if ( m[newY][newX] == '#' )
{
if ( m[newY][oldX] == '#' ) dir.y = -dir.y;
if ( m[oldY][newX] == '#' ) dir.x = -dir.x;
if ( m[newY][oldX] != '#' && m[oldY][newX] != '#' )
break; // ray destroyed
}
//}
//else if ( isInt( newPos.x ) )
//{
// int Y = ;
// if ( m[Y][newX] == '#' ) continue;
//}
pos = newPos;
}
}
return ret;
}
void main()
{
FileReader fin( "D-small-attempt0.in" );
FileWriter fout( "out.txt" );
int T = fin.readInt();
fin.readLine();
for ( int i = 0; i < T; i++ )
{
stringstream ss;
ss << "Case #" << i+1 << ": " << doit( fin ) << endl;
fout << ss.str().c_str();
OutputDebugStringA( ss.str().c_str() );
}
} | 0 |
C10132 | C10144 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#define REP(i,n) for(int i=0; i<(int)(n); i++)
inline int getInt(){ int s; scanf("%d", &s); return s; }
using namespace std;
char g[64][64];
int main(){
int t = getInt();
REP(cc, t){
int h = getInt();
int w = getInt();
int d = getInt();
REP(i,h) scanf("%s", g[i]);
double sx, sy;
REP(i,h) REP(j,w){
if(g[i][j] == 'X'){
sx = j + 0.5; sy = i + 0.5;
g[i][j] = '.';
break;
}
}
vector<double> ans;
const double PI = 2 * acos(0);
const double EPS = 1e-10;
int lim = 2 * max(d, max(h, w));
for(int xx = -lim; xx <= lim; xx++){
for(int yy = -lim; yy <= lim; yy++) if(xx || yy){
double ds = sqrt(xx * xx + yy * yy);
double dx = xx / ds;
double dy = yy / ds;
// printf("[%d %d]\n", xx, yy);
double x = sx;
double y = sy;
double dist = 0.0;
while(true){
double nt = 10000.0;
double nx = 10000.0;
if(dx > EPS) nx = ceil(x * 2 + EPS) / 2;
else if(dx < -EPS) nx = floor(x * 2 - EPS) / 2;
if(std::abs(dx) > EPS) nt = min(nt, (nx - x) / dx);
double ny = 10000.0;
if(dy > EPS) ny = ceil(y * 2 + EPS) / 2;
else if(dy < -EPS) ny = floor(y * 2 - EPS) / 2;
if(std::abs(dy) > EPS) nt = min(nt, (ny - y) / dy);
x += dx * nt;
y += dy * nt;
dist += sqrt(nt * nt * dx * dx + nt * nt * dy * dy);
// printf(" %.2f %.2f: %.2f\n", x, y, dist);
if(dist > d + EPS) break;
if(std::abs(sx - x) + std::abs(sy - y) < EPS){
ans.push_back(atan2(dx, dy));
// printf(" found!!\n");
}
bool xf = g[(int)(y - dy * EPS)][(int)(x + dx * EPS)] == '#';
bool yf = g[(int)(y + dy * EPS)][(int)(x - dx * EPS)] == '#';
bool xyf = g[(int)(y + dy * EPS)][(int)(x + dx * EPS)] == '#';
if(xyf){
if(xf && yf){
dx *= -1; dy *= -1;
}else{
if(xf){
dx *= -1;
}else if(yf){
dy *= -1;
}else{
break;
}
}
}
}
}
}
sort(ans.begin(), ans.end());
int cnt = 0;
REP(i,ans.size()){
if(i == 0){
cnt++;
// printf("%.2f ", ans[i]);
}else if(std::abs(ans[i - 1] - ans[i]) > EPS &&
std::abs(ans[i] - (ans[0] + 2 * PI)) > EPS){
// printf("%.2f ", ans[i]);
cnt++;
}
}
// printf("\n\n\n\n\n\n");
printf("Case #%d: %d\n", cc + 1, cnt);
}
return 0;
}
| 0 |
C10132 | C10236 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned int UINT32;
vector<pair<int,int> > getMirrors(int r, int c, int H, int W)
{
vector<pair<int,int> > q;
q.push_back(make_pair(-r,c));
q.push_back(make_pair(2*H-r,c));
q.push_back(make_pair(r,-c));
q.push_back(make_pair(r,2*W-c));
return q;
}
int dist2(int x1, int y1, int x2, int y2)
{
return (x1 - x2) * (x1 - x2) + (y1-y2)*(y1-y2);
}
string calc()
{
int H, W, D;
cin >> H >> W >> D;
vector<string> grid(H);
int r, c;
for (int i = 0; i < H; ++i) {
cin >> grid[i];
for (int j = 0; j < W; ++j) {
if (grid[i][j] == 'X') {
r = i;
c = j;
}
}
}
r = 2*r - 1;
c = 2*c - 1;
H = 2*(H - 1) - 2;
W = 2*(W - 1) - 2;
D *= 2;
set<pair<int,int> > visited;
queue<pair<int,int> > q;
vector<pair<int,int> > mirrors = getMirrors(r, c, H, W);
for (int i = 0; i < 4; ++i) {
pair<int, int> m = mirrors[i];
if (dist2(m.first, m.second, r, c) <= D*D) {
visited.insert(m);
q.push(m);
}
}
visited.insert(make_pair(r,c));
//cout << "r/c: " << r << ' ' << c << endl;
//cout << "H/W: " << H << ' ' << W << endl;
vector<pair<int,int> > candidates;
while (!q.empty()) {
pair<int, int> cur = q.front(); q.pop();
candidates.push_back(cur);
int r1 = cur.first - r;
int c1 = cur.second - c;
mirrors = getMirrors(cur.first, cur.second, H, W);
for (int i = 0; i < 4; ++i) {
pair<int, int> m = mirrors[i];
int r2 = m.first - r;
int c2 = m.second - c;
if (r1*c2 - c1*r2 == 0) continue;
if (!visited.count(m) && dist2(m.first, m.second, r, c) <= D*D) {
visited.insert(m);
q.push(m);
}
}
}
/*
for (int i = 0; i < candidates.size(); ++i) {
cout << candidates[i].first << ' ' << candidates[i].second << endl;
}
*/
sort(candidates.begin(), candidates.end());
for (int i = candidates.size() - 1; i >= 0; --i) {
int r1 = candidates[i].first - r;
int c1 = candidates[i].second - c;
//for (int j = i -1; j>=0; --j) {
for (int j = 0; j < candidates.size(); ++j) if (j != i) {
int r2 = candidates[j].first - r;
int c2 = candidates[j].second - c;
if (r1*c2 - c1*r2 != 0) continue;
int r3 = candidates[j].first - candidates[i].first;
int c3 = candidates[j].second - candidates[i].second;
if (r1*r1 + c1*c1 >= r2*r2 + c2*c2 && r3*r3+c3*c3 <= r1*r1+c1*c1) {
candidates.erase(candidates.begin() + i);
//cout << "erase" << endl;
break;
}
}
}
for (int i = 0; i < candidates.size(); ++i) {
//cout << candidates[i].first << ' ' << candidates[i].second << endl;
}
stringstream S;
S << candidates.size();
return S.str();
}
int main(void)
{
int N;
cin >> N;
// NOTE: if using getline() to read the input, the following two lines should be
// added to read the line sepeartor in the first line.
//string line;
//getline(cin, line);
for (int i=1; i<=N; ++i) {
cout << "Case #" << i << ": " << calc() << endl;
}
return 0;
}
| 0 |
C10132 | C10298 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <cstdlib>
using namespace std;
const int DIM_MAX = 35, D_MAX = 55;
int H, W, D;
int h_scale, w_scale;
int you_x, you_y;
bool base_grid[DIM_MAX][DIM_MAX];
bool nav_info[3][3];
bool direct_reflection(int dx, int dy) {
int cur_x = you_x, cur_y = you_y;
int dist = -1;
while (!base_grid[cur_x][cur_y]) {
cur_x += dx;
cur_y += dy;
dist += 2;
}
return (dist <= D);
}
void prepare_nav_info(int x, int y) {
nav_info[0][0] = base_grid[(x - 1) / h_scale][(y - 1) / w_scale];
nav_info[0][2] = base_grid[(x - 1) / h_scale][y / w_scale];
nav_info[2][0] = base_grid[x / h_scale][(y - 1) / w_scale];
nav_info[2][2] = base_grid[x / h_scale][y / w_scale];
}
bool is_obstacle(int dx, int dy) {
return nav_info[dx + 1][dy + 1];
}
bool test_dest(int x, int y) {
// cout << "Testing " << x << ", " << y << endl;
int dir_x = (x > 0 ? 1 : -1);
int dir_y = (y > 0 ? 1 : -1);
x = abs(x); y = abs(y);
h_scale = 2 * y; w_scale = 2 * x;
int start_x = y * (2 * you_x + 1);
int start_y = x * (2 * you_y + 1);
int num_steps = h_scale * w_scale / 2;
int cur_x = start_x, cur_y = start_y;
for (int i = 0; i < num_steps; i++) {
// cout << "(" << cur_x << ", " << cur_y << ")\n";
if (i > 0 && cur_x == start_x && cur_y == start_y)
return false;
// WILL THIS WORK!?!?!
prepare_nav_info(cur_x, cur_y);
if (is_obstacle(dir_x, dir_y)) {
bool obstacle_x = is_obstacle(-dir_x, dir_y);
bool obstacle_y = is_obstacle(dir_x, -dir_y);
if ((!obstacle_x) && (!obstacle_y))
return false;
if (obstacle_x)
dir_y = -dir_y;
if (obstacle_y)
dir_x = -dir_x;
}
cur_x += dir_x; cur_y += dir_y;
}
return (cur_x == start_x && cur_y == start_y);
}
void solve_case(int case_num) {
cin >> H >> W >> D;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
char c;
cin >> c;
base_grid[i][j] = (c == '#');
if (c == 'X') {
you_x = i; you_y = j;
}
}
}
int num_reflections = 0;
for (int i = -D_MAX; i <= D_MAX; i++) {
for (int j = -D_MAX; j <= D_MAX; j++) {
if (i == 0 || j == 0 || i * i + j * j > D * D)
continue;
num_reflections += (test_dest(i, j) ? 1 : 0);
}
}
num_reflections += (direct_reflection(0, 1) ? 1 : 0);
num_reflections += (direct_reflection(0, -1) ? 1 : 0);
num_reflections += (direct_reflection(1, 0) ? 1 : 0);
num_reflections += (direct_reflection(-1, 0) ? 1 : 0);
cout << "Case #" << case_num + 1 << ": " << num_reflections << '\n';
}
int main() {
int T;
cin >> T;
for (int i = 0; i < T; i++) {
solve_case(i);
}
}
| 0 |
C10132 | C10255 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <assert.h>
#include <cmath>
#include <set>
using namespace std;
#define FOR(i,n) for (int i = 0; i < n; i++)
#define abs(x) ((x)<0?(-(x)):(x))
#define REP(i,v) for (unsigned i = 0; i < v.size(); i++)
#define RL(i,v) for (unsigned i = 0; i < v.length(); i++)
typedef long long ll;
struct PtInfo {
int x, y;
int gx, gy;
int distSqr;
bool operator < (const PtInfo& rhs) const { return distSqr < rhs.distSqr; }
};
static inline int sqr(int x) { return x*x; }
int gcd(int a, int b)
{
while (a && b) {
b %= a;
if (b) a %= b;
}
return a+b;
}
void solve(void)
{
set<pair<int, int> > ss;
int W, H, D;
char s[100];
scanf("%d%d%d", &H, &W, &D);
int ix, iy;
FOR(y, H) {
scanf("%s", s);
FOR(x, W) {
if (s[x] == 'X') {
ix = x*2 - 1;
iy = y*2 - 1;
}
}
}
W = (W - 2) * 2;
H = (H - 2) * 2;
D *= 2;
vector<PtInfo> v;
for (int i = -64; i <= 64; i++)
for (int j = -64; j <= 64; j++) {
if (i == 0 && j == 0) continue;
PtInfo p;
p.gx = j;
p.gy = i;
p.x = j * W;
p.y = i * H;
if ((j + 100) % 2) p.x += W - ix;
else p.x += ix;
if ((i + 100) % 2) p.y += H - iy;
else p.y += iy;
p.distSqr = sqr(p.x - ix) + sqr(p.y - iy);
if (p.distSqr <= D*D)
v.push_back(p);
}
sort(v.begin(), v.end());
REP(ii, v) {
int dx = v[ii].x - ix;
int dy = v[ii].y - iy;
int G = gcd(abs(dx), abs(dy));
dx /= G;
dy /= G;
pair<int, int> key = make_pair(dx, dy);
if (ss.find(key) == ss.end()) {
ss.insert(key);
// printf("At (gx = %d, gy = %d, x = %d, y = %d), dist = %.3lf\n", v[ii].gx, v[ii].gy, v[ii].x, v[ii].y, sqrt(v[ii].distSqr));
}
}
printf("%d\n", (int) ss.size());
}
int main(void)
{
// freopen("/home/vesko/gcj/d.in", "rt", stdin);
int T;
scanf("%d", &T);
for (int tc = 1; tc <= T; tc++) {
printf("Case #%d: ", tc);
solve();
}
return 0;
}
| 0 |
C10132 | C10191 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#define N 1000010
using namespace std;
char room[40][40];
int h, w, d;
struct Point {
int x, y;
Point (int _x = 0, int _y = 0) {
x = _x, y = _y;
}
Point set(int _x = 0, int _y = 0) {
x = _x, y = _y;
return *this;
}
bool operator < (const Point &a) const {
return x < a.x || x == a.x && y < a.y;
}
int dist2(const Point &a) const {
return (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y);
}
Point slope(const Point &a) {
int dx = x - a.x, dy = y - a.y;
int g = max(1, gcd(abs(dx), abs(dy)));
return Point(dx / g, dy / g);
}
private:
static int gcd(int _x, int _y) {
return _y ? gcd(_y, _x % _y): _x;
}
}you;
int check(int x, int y, set<Point>& imageSlope, int &ans) {
Point p(x, y);
Point pSlope = p.slope(you);
if(p.dist2(you) <= d * d && imageSlope.find(pSlope) == imageSlope.end()) {
++ans;
imageSlope.insert(pSlope);
}
return ans;
}
int solve() {
int ans = 0;
set<Point> imageSlope;
for(int i = 0; i <= (d + 2 * you.x) / h; i += 2) {
for(int j = 0; j <= (d + 2 * you.y) / w; j += 2) {
for(int k = 0; k < 16; ++k) {
int c[4];
for(int ii = 0; ii < 4; ++ii) {
c[ii] = ((k & (1<<ii)) ? -1: 1);
}
check(c[0] * i * h + c[1] * you.x, c[2] * j * w + c[3] * you.y, imageSlope, ans);
}
}
}
return ans - 1;
}
int main(int argc, char *argv[])
{
freopen("D-small-attempt3.in", "r", stdin);
freopen("D-small-attempt3.out", "w", stdout);
int T;
cin >> T;
for(int testcase = 1; testcase <= T; ++testcase) {
cout << "Case #" << testcase << ": ";
cin >> h >> w >> d;
for(int i = 0; i < h; ++i) {
cin >> room[i];
for(int j = 1; j < w; ++j) {
if(room[i][j] == 'X') {
you.set((i - 1) * 2 + 1, (j - 1) * 2 + 1);
}
}
}
h = (h - 2) * 2, w = (w - 2) * 2, d = d * 2;
int ans = solve();
cout << ans << endl;
}
return 0;
}
| 0 |
C10132 | C10087 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <algorithm>
#include <limits>
#include <sstream>
#include <string>
#include <numeric>
#include <iterator>
#include <boost/rational.hpp>
using namespace std;
using namespace boost;
#define PRINT(E)\
do {\
cerr << #E ": " << (E) << endl;\
}\
while(0)
#define PRINTR(E)\
do {\
cerr << #E ": [";\
for (const auto& i: (E)) cerr << i << " ";\
cerr << "]" << endl;\
}\
while(0)
template <class T, class D>
class Range {
class It {
public:
It(const T& v,
const T& e,
const D& s)
: v_(v),
e_(e),
s_(s),
end_(false)
{
checkEnd();
};
T operator*() {
return v_;
}
It& operator++() {
if (!end_) {
v_ += s_;
checkEnd();
}
return *this;
}
It operator++(int) {
It r = *this;
++r;
return r;
}
bool operator!=(const It& o) const {
return (end_ && !o.end_) || (!end_ && o.end_)
|| (!end_ && (v_ != o.v_));
}
private:
void checkEnd() {
bool pos = (s_ > static_cast<D>(0));
bool eq = v_ == e_;
bool more = v_ > e_;
end_ = eq || (more && pos) || (!more && !pos);
}
T v_;
T e_;
D s_;
bool end_;
};
public:
Range(const T& b, const T& e, const D& s): b_(b), e_(e), s_(s) {};
It begin() const { return It{b_, e_, s_}; }
It end() const { return It{e_, e_, s_}; }
private:
T b_;
T e_;
D s_;
};
template <typename T>
Range<T, T> R(const T& e) {
typedef decltype(e - e) D;
return Range<T, T>{static_cast<T>(0), e, static_cast<D>(1)};
}
template <typename T>
Range<T, T> R(const T& b, const T& e) {
typedef decltype(e - b) D;
return Range<T, T>{b, e, static_cast<D>(1)};
}
template <typename T, typename D>
Range<T, D> R(const T& b, const T& e, const D& d) {
return Range<T, D>{b, e, d};
}
template <class T>
T read() {
T r;
cin >> r;
return r;
}
template <typename T>
int sign(T n) {
return (0 < n) - (n < 0);
}
int abs(int n) {
return (n >= 0) ? n : -n;
}
typedef rational<int> Rint;
typedef pair<Rint, Rint> Point;
class Direction {
public:
Direction(int x, int y) {
assert(x != 0 || y != 0);
if (x == 0) {
a = 0;
b = sign(y);
} else if (y == 0) {
a = sign(x);
b = 0;
} else {
a.assign(x, abs(y));
b = sign(y);
}
}
bool operator<(const Direction& other) const {
return (a < other.a) || ((a == other.a) && (b < other.b));
}
Rint a;
Rint b;
};
bool isInt(const Rint& r) {
return r.denominator() == 1;
}
ostream& operator<<(ostream& os, const Direction& dir) {
os << "(" << dir.a << ", " << dir.b << ")";
return os;
}
vector<Direction> createDirs(int D) {
set<Direction> dirs;
for (int i: R(-D, D + 1)) {
for (int j: R(-D, D + 1)) {
if (i == 0 && j == 0) continue;
if (i * i + j * j > D * D) continue;
dirs.insert(Direction(i, j));
}
}
return {dirs.begin(), dirs.end()};
}
bool solve(const Point& s, const Direction& dir, const vector<string>& field, int D) {
int x = s.first.numerator() / s.first.denominator();
int y = s.second.numerator() / s.second.denominator();
if (dir.a == 0) {
int step = sign(dir.b);
int dist = 1;
while (field[x][y + step] != '#') {
y += step;
dist += 2;
}
return dist <= D;
}
if (dir.b == 0) {
int step = sign(dir.a);
int dist = 1;
while (field[x + step][y] != '#') {
x += step;
dist += 2;
}
return dist <= D;
}
bool start = true;
Point p = s;
Direction d = dir;
Rint x_t = 0;
Rint y_t = 0;
while (x_t * x_t + y_t * y_t <= D * D) {
if (!start && field[x][y] == 'X') {
Rint x_c = (x + Rint(1, 2)) - p.first;
Rint y_c = (y + Rint(1, 2)) - p.second;
if (x_c * d.b - y_c * d.a == 0) {
x_t += abs(x_c);
y_t += abs(y_c);
return x_t * x_t + y_t * y_t <= D * D;
}
}
int x_p = (d.a > 0) ? (x + 1) : x;
int y_p = (d.b > 0) ? (y + 1) : y;
Rint t_x = (x_p - p.first) / d.a;
Rint t_y = (y_p - p.second) / d.b;
assert(t_x > 0);
assert(t_y > 0);
auto t = min(t_x, t_y);
auto dx = t * d.a;
auto dy = t * d.b;
x_t += abs(dx);
p.first += dx;
y_t += abs(dy);
p.second += dy;
if (t_x < t_y) {
if (field[x + sign(d.a)][y] == '#') {
d.a = -d.a;
} else {
x += sign(d.a);
}
} else if (t_y < t_x) {
if (field[x][y + sign(d.b)] == '#') {
d.b = -d.b;
} else {
y += sign(d.b);
}
} else {
int di = sign(d.a);
int dj = sign(d.b);
if (field[x + di][y] != '#' &&
field[x][y + dj] != '#' &&
field[x + di][y + dj] == '#')
{
return false;
}
if (field[x + di][y] == '#' &&
field[x + di][y + dj] == '#')
{
d.a = -d.a;
} else {
x += di;
}
if (field[x][y + dj] == '#' &&
field[x + di][y + dj] == '#')
{
d.b = -d.b;
} else {
y += dj;
}
}
start = false;
}
return false;
}
int solve(const vector<string>& field, int D) {
Point s;
for (int i: R(field.size())) {
for (int j: R(field[0].size())) {
if (field[i][j] == 'X') {
s = Point(i + Rint(1, 2), j + Rint(1, 2));
break;
}
}
}
int res = 0;
for (const auto& dir: createDirs(D)) {
res += solve(s, dir, field, D);
}
return res;
}
int main() {
int testCount = read<int>();
for (auto TEST: R(1, testCount + 1)) {
int H, W, D;
cin >> H >> W >> D;
vector<string> field(H);
for (int i: R(H)) {
cin >> field[i];
}
cout << "Case #" << TEST << ": " << solve(field, D) << endl;
}
return 0;
}
| 0 |
C10132 | C10053 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <set>
#include <fstream>
#include <string>
using namespace std;
typedef pair<int, int> pii;
namespace {
static int GCD(int a, int b) { return b==0 ? a : GCD(b,a%b); }
};
int main()
{
ifstream fin("D-small-attempt0.in");
ofstream fout("D-small-attempt0.out");
unsigned int numberOfCases;
fin >> numberOfCases;
int H,W,D;
string s;
for (unsigned int zz=1; zz<=numberOfCases; ++zz)
{
fin >> H >> W >> D;
getline(fin, s);
int h1=-1, w1=-1;
for (int i=0; i<H; ++i)
{
getline(fin, s);
int loc = s.find('X');
if (loc != string::npos)
h1 = i-1, w1 = loc - 1;
}
H-=2;
W-=2;
int maxSqrDist = D*D;
set<pii> results;
for (int i=-51; i<=51; ++i)
{
bool reflectH = (i%2)!=0;
int h2 = (reflectH ? H-1-h1 : h1) + H * i;
int dh = h2-h1;
for (int j=-51; j<=51; ++j)
{
bool reflectW = (j%2)!=0;
int w2 = (reflectW ? W-1-w1 : w1) + W * j;
int dw = w2-w1;
int sqrDist = dh*dh + dw*dw;
if (sqrDist <= maxSqrDist && sqrDist > 0)
{
int z = GCD(abs(dh), abs(dw));
results.insert(make_pair(dh/z, dw/z));
}
}
}
fout << "Case #" << zz << ": " << results.size() << endl;
}
return 0;
} | 0 |
C10132 | C10272 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <fstream>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <functional>
#include <limits>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
const double pi = acos(-1.0);
#define oned(a, x1, x2) { cout << #a << ":"; for(int _i = (x1); _i < (x2); _i++){ cout << " " << a[_i]; } cout << endl; }
#define twod(a, x1, x2, y1, y2) { cout << #a << ":" << endl; for(int _i = (x1); _i < (x2); _i++){ for(int _j = (y1); _j < (y2); _j++){ cout << (_j > y1 ? " " : "") << a[_i][_j]; } cout << endl; } }
#define mp(x, y) make_pair(x, y)
int n, m, d, t[205][205], vis[205][205], X, Y;
/*enum {
UP, RIGHT, DOWN, LEFT
};
bool eq(double a, double b) {
return fabs(a-b) < 1e-9;
}*/
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
void solve(int test) {
int ka = 200/n, kb = 200/m;
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
for(int a = 0; a < ka; a++) {
for(int b = 0; b < kb; b++) {
t[(a%2?n-x-1:x)+a*n][(b%2?m-y-1:y)+b*m] = t[x][y];
}
}
}
}
int qa = ka/2, qb = kb/2;
if(qa%2) qa--;
if(qb%2) qb--;
X += qa*n;
Y += qb*m;
int res = 0;
n *= ka;
m *= kb;
// cout << X << ' ' << Y << " : " << n << ' ' << m << endl;
memset(vis, 0, sizeof(vis));
set<pair<int,int> > cnt;
for(int x = X-d; x <= X+d; x++) {
for(int y = Y-d; y <= Y+d; y++) {
if(t[x][y] == 1 && (x!=X || y !=Y) && (x-X)*(x-X)+(y-Y)*(y-Y)<=d*d) {
int g = gcd(abs(x-X), abs(y-Y));
cnt.insert(mp((x-X)/g, (y-Y)/g));
}
}
}
// twod(t, X-d, X+d+1, Y-d, Y+d+1);
cout << "Case #" << test << ": " << cnt.size() << endl;
}
int main() {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
int T; scanf("%d\n", &T);
for(int test = 1; test <= T; test++) {
cin >> n >> m >> d;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
char c; cin >> c;
if(c == 'X') {
X = i-1;
Y = j-1;
t[i-1][j-1] = 1;
} else if(c == '.') {
t[i-1][j-1] = 0;
}
}
}
n-=2;
m-=2;
solve(test);
}
}
| 0 |
C10132 | C10038 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <stdio.h>
#define SQR(x) ((x) * (x))
inline int sign(int x) {
if (x == 0) return 0;
if (x > 0) return 1;
if (x < 0) return -1;
}
inline int abs(int x) {
if (x > 0) return x; else return -x;
}
const int MAXN = 60;
int n, m, d;
char map[MAXN][MAXN];
int posx, posy;
int pts[20000][2];
int main() {
int cases;
scanf("%d", &cases);
for (int k = 0; k < cases; ++k) {
scanf("%d%d%d", &n, &m, &d);
for (int i = 0; i < n; ++i) scanf("%s", map[i]);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (map[i][j] == 'X') {
posx = i, posy = j;
}
}
}
int px = 2 * (posx - 1) + 1, py = 2 * (posy - 1) + 1;
n = 2 * (n - 2), m = 2 * (m - 2);
fprintf(stderr, "n = %d, m = %d, px = %d, py = %d\n", n, m, px, py);
int top = 0;
for (int i = -d * 2; i <= d * 2; ++i) {
for (int j = -d * 2; j <= d * 2; ++j) {
int newx, newy;
newx = n * i, newy = m * j;
if (SQR(newx) + SQR(newy) <= SQR(d)) {
pts[top][0] = newx, pts[top][1] = newy;
++top;
}
newx = n * i - px, newy = m * j;
if (SQR(newx) + SQR(newy) <= SQR(d)) {
pts[top][0] = newx, pts[top][1] = newy;
++top;
}
newx = n * i, newy = m * j - py;
if (SQR(newx) + SQR(newy) <= SQR(d)) {
pts[top][0] = newx, pts[top][1] = newy;
++top;
}
newx = n * i - px, newy = m * j - py;
if (SQR(newx) + SQR(newy) <= SQR(d)) {
pts[top][0] = newx, pts[top][1] = newy;
++top;
}
}
}
fprintf(stderr, "top = %d\n", top);
for (int i = 0; i < top; ++i) {
// fprintf(stderr, "(%d, %d)\n", pts[i][0], pts[i][1]);
}
int ans = 0;
for (int i = 0; i < top; ++i) {
bool flag = true;
for (int j = 0; j < top; ++j) {
if (i == j) continue;
if (sign(pts[i][0]) != sign(pts[j][0])) continue;
if (sign(pts[i][1]) != sign(pts[j][1])) continue;
if (abs(pts[i][0]) < abs(pts[j][0])) continue;
if (abs(pts[i][1]) < abs(pts[j][1])) continue;
if (pts[i][0] * pts[j][1] == pts[i][1] * pts[j][0]) {
flag = false;
break;
}
}
if (flag)
++ans;
// else {printf("---(%d, %d)\n", pts[i][0], pts[i][1]); }
}
printf("Case #%d: %d\n", k + 1, ans - 1);
}
return 0;
}
| 0 |
C10132 | C10284 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <cstdlib>
using namespace std;
bool within_range(long long int x_, long long int y_, long long int x, long long int y, long long int d)
{
return (x-x_)*(x-x_)+(y-y_)*(y-y_) <= d*d;
}
int sgn(int n)
{
if (n < 0)
{
return -1;
}
if (n > 0)
{
return 1;
}
return 0;
}
bool check_valid(const vector<pair<int, int> >& all_points, int index, int x, int y)
{
int n = all_points.size();
int i;
for (i = 0; i < n; i++)
{
if (i == index)
{
continue;
}
if (sgn(all_points[i].first-x) != sgn(all_points[index].first-x))
{
continue;
}
if (sgn(all_points[i].second-y) != sgn(all_points[index].second-y))
{
continue;
}
if (abs(all_points[i].first-x) > abs(all_points[index].first-x))
{
continue;
}
if (abs(all_points[i].second-y) > abs(all_points[index].second-y))
{
continue;
}
if (abs((all_points[i].first-x) * (all_points[index].second-y)) == abs((all_points[i].second-y) * (all_points[index].first-x)))
{
return false;
}
}
return true;
}
int main()
{
int t, T;
int H, W;
int D;
long long int ans;
int i, j;
int x, y;
int x_, y_;
string s;
vector<pair<int, int> > all_points;
cin >> T;
for (t = 1; t <= T; t++)
{
cin >> H >> W >> D;
for (i = 0; i < H; i++)
{
cin >> s;
for (j = 0; j < W; j++)
{
if (s[j] == 'X')
{
x = j;
y = i;
}
}
}
// Re-scale
x = 2 * (x - 1) + 1;
y = 2 * (y - 1) + 1;
H = 2 * (H - 2);
W = 2 * (W - 2);
D = 2 * D;
ans = 0;
all_points.clear();
for (i = -100; i <= 100; i++)
{
for (j = -100; j <= 100; j++)
{
if (i == 0 && j == 0)
{
continue;
}
if (i == 0 && j > 1)
{
continue;
}
if (i == 0 && j < -1)
{
continue;
}
if (j == 0 && i > 1)
{
continue;
}
if (j == 0 && i < -1)
{
continue;
}
// calculate x_, y_
if (i % 2 == 0)
{
x_ = i * W + x;
}
else
{
x_ = i * W + (W - x);
}
if (j % 2 == 0)
{
y_ = j * H + y;
}
else
{
y_ = j * H + (H - y);
}
if (within_range(x, y, x_, y_, D))
{
all_points.push_back(make_pair(x_, y_));
}
}
}
for (size_t s = 0; s < all_points.size(); s++)
{
ans += check_valid(all_points, s, x, y);
}
cout << "Case #" << t << ": " << ans << endl;
}
return 0;
}
| 0 |
C10132 | C10252 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<numeric>
#include<math.h>
#include<set>
#include<map>
#include<fstream>
#define epsilon 0.000000001
#define cosinusa(a, b, c) ((a * a + b * b - c * c) / (2.0 * a * b));
#define infi 1000000000
using namespace std;
vector<string> matrix;
int n, m;
double targX, targY;
double my_distance(const pair<double, double>& from, const pair<double, double>& target)
{
return sqrt((from.first - target.first) * (from.first - target.first) + (from.second - target.second) * (from.second - target.second));
}
int isVisible(const pair<double, double>& from, const pair<double, double>& target)
{
if (fabs(from.first - target.first) < epsilon && fabs(from.second - target.second) < epsilon)
return fabs(from.first - targX) < epsilon && fabs(from.second - targY) < epsilon ? 0 : -1;
pair<double, double> xPoint, yPoint;
if (from.first > target.first + epsilon)
{
double x = floor(from.first - epsilon);
double y = from.second + ((x - from.first) / (target.first - from.first)) * (target.second - from.second);
xPoint = make_pair(x, y);
} else if (from.first + epsilon < target.first)
{
double x = ceil(from.first + epsilon);
double y = from.second + ((x - from.first) / (target.first - from.first)) * (target.second - from.second);
xPoint = make_pair(x, y);
}
if (from.second > target.second + epsilon)
{
double y = floor(from.second - epsilon);
double x = from.first + ((y - from.second) / (target.second - from.second)) * (target.first - from.first);
yPoint = make_pair(x, y);
} else if (from.second + epsilon < target.second)
{
double y = ceil(from.second + epsilon);
double x = from.first + ((y - from.second) / (target.second - from.second)) * (target.first - from.first);
yPoint = make_pair(x, y);
}
double distX = my_distance(from, xPoint);
double distY = my_distance(from, yPoint);
double dist3 = my_distance(from, target);
if (dist3 < distX + epsilon && dist3 < distY + epsilon)
return fabs(target.first - targX) < epsilon && fabs(target.second - targY) < epsilon ? 0 : -1;
int tmp;
if(fabs(from.first - target.first) < epsilon || distY + epsilon < distX)
{
int i = (yPoint.first < -epsilon) ? (int) (yPoint.first - epsilon) : (int)(yPoint.first + epsilon);
int j = (yPoint.second < -epsilon) ? (int) (yPoint.second - epsilon) : (int)(yPoint.second + epsilon);
if (matrix[i][j] == '#' || matrix[i][j - 1] == '#')
{
double x = target.first;
double y = yPoint.second - (target.second - yPoint.second);
tmp = isVisible(yPoint, make_pair(x, y));
return tmp != -1 ? 1 + tmp : -1;
}
else
{
return isVisible(yPoint, target);
}
}
if(fabs(from.second - target.second) < epsilon || distX + epsilon < distY)
{
int i = (xPoint.first < -epsilon) ? (int) (xPoint.first - epsilon) : (int)(xPoint.first + epsilon);
int j = (xPoint.second < -epsilon) ? (int) (xPoint.second - epsilon) : (int)(xPoint.second + epsilon);
if (matrix[i][j] == '#' || matrix[i - 1][j] == '#')
{
double y = target.second;
double x = xPoint.first - (target.first - xPoint.first);
tmp = isVisible(xPoint, make_pair(x, y));
return tmp != -1 ? 1 + tmp : -1;
}
else
{
return isVisible(xPoint, target);
}
}
int x = (xPoint.first < -epsilon) ? (int) (xPoint.first - epsilon) : (int)(xPoint.first + epsilon);
int y = (xPoint.second < -epsilon) ? (int) (xPoint.second - epsilon) : (int)(xPoint.second + epsilon);
int mat[4][2] = {{0, 0}, {-1, 0}, {0, -1}, {-1, -1}};
int cnt = 0;
for (int i = 0; i < 4; i++)
cnt += matrix[x + mat[i][0]][y + mat[i][1]] == '#';
if (cnt == 0 || (cnt == 2 && matrix[x][y] == matrix[x - 1][y - 1]))
return isVisible(xPoint, target);
if (cnt == 1)
return -1;
if (cnt == 3)
{
double x = 2 * xPoint.first - target.first;
double y = 2 * xPoint.second - target.second;
tmp = isVisible(xPoint, make_pair(x, y));
return tmp != -1 ? 1 + tmp : -1;
}
// cnt == 2, two next to each other
if (matrix[x][y] == matrix[x][y - 1])
{
double y = target.second;
double x = xPoint.first - (target.first - yPoint.first);
tmp = isVisible(xPoint, make_pair(x, y));
return tmp != -1 ? 1 + tmp : -1;
}
else
{
double x = target.first;
double y = yPoint.second - (target.second - yPoint.second);
tmp = isVisible(yPoint, make_pair(x, y));
return tmp != -1 ? 1 + tmp : -1;
}
}
int main()
{
freopen("google.in", "r", stdin);
freopen("google.out", "w", stdout);
int numTests;
cin >> numTests;
for(int testCounter = 0; testCounter < numTests; ++testCounter)
{
printf("Case #%d: ", testCounter + 1);
matrix.clear();
cin >> n >> m;
int d;
cin >> d;
matrix.resize(n);
for (int i = 0; i < n; i++)
{
cin >> matrix[i];
}
int x, y;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if(matrix[i][j] == 'X')
{
matrix[i][j] = '.';
x = i;
y = j;
}
}
targX = x + 0.5;
targY = y + 0.5;
int cnt = 0;
vector<pair<int, int> > solutions;
int tmp;
for (int i = -51; i <= 51; i++)
{
for (int j = -51; j <= 51; j++)
{
if(i * i + j * j <= d * d)
{
tmp = isVisible(make_pair((double)x + 0.5, (double)y + 0.5), make_pair((double)x + i + 0.5, (double)y + j + 0.5));
if(tmp > 0)
{
solutions.push_back(make_pair(i, j));
}
}
}
}
for (int i = 0; i < solutions.size(); i++)
{
bool does = true;
for (int j = 0; j < i; j++)
{
if (solutions[i].first * solutions[j].second == solutions[i].second * solutions[j].first
&& solutions[i].first * solutions[j].first >= 0
&& solutions[i].second * solutions[j].second >= 0)
{
does = false;
break;
}
}
if(does)
cnt++;
}
cout << cnt << endl;
}
return 0;
}
| 0 |
C10132 | C10029 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include "StdAfx.h"
#include "GoogleCase.h"
GoogleCase::GoogleCase()//)QObject* parent /*= 0*/)
// : QObject(parent)
{
}
GoogleCase::~GoogleCase()
{
}
| 0 |
C10132 | C10262 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | // mirror.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <deque>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream cin(argv[1]);
std::ofstream cout(argv[2]);
int T;
cin>>T;
char grid[50][50];
for(int i = 0 ; i < T ; i++){
int H, W;
double D;
cin>>H>>W>>D;
int num = 0;
double x,y;
for(int j = 0 ; j< H ; j++){
for( int k = 0 ; k < W ; k++){
cin>>grid[j][k];
if(grid[j][k] == 'X'){
y = j+0.5;
x = k+0.5;
}
}
}
//cout<<x<<" "<<y<<endl;
double a1 = 1, b1 = 1;
double a2 = W- 1, b2 = 1;
double a3 = 1, b3 = H - 1;
double a4 = W - 1, b4 = H - 1;
deque<double> xx;
deque<double> yy;
vector<double> xxx;
vector<double> yyy;
xx.push_back(x);
yy.push_back(y);
double tx = xx.front();
double ty = yy.front();
xx.pop_front();
yy.pop_front();
if(ty > b1){
double kx = tx;
double ky = ty - (ty - b1)*2;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(ty < b3){
double kx = tx;
double ky = ty + (b3 - ty)*2;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(tx > a1){
double kx = tx - (tx-1)*2;
double ky = ty ;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(tx < a4){
double kx = tx + (a4-tx)*2;
double ky = ty ;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
while(xx.size() != 0){
double tx = xx.front();
double ty = yy.front();
xx.pop_front();
yy.pop_front();
if(ty > b1){
double kx = tx;
double ky = ty - (ty - b1)*2;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D && (ty- y)*(tx - kx) != (ty - ky)*(tx-x) ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(ty < b3){
double kx = tx;
double ky = ty + (b3 - ty)*2;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D &&(ty- y)*(tx - kx) != (ty - ky)*(tx-x)){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(tx > a1){
double kx = tx - (tx-1)*2;
double ky = ty ;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D && (ty- y)*(tx - kx) != (ty - ky)*(tx-x) ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
if(tx < a4){
double kx = tx + (a4-tx)*2;
double ky = ty ;
if( sqrt((kx-x)*(kx-x) + (ky - y)*(ky-y)) <= D && (ty- y)*(tx - kx) != (ty - ky)*(tx-x) ){
int mm;
for(mm = 0 ; mm < xxx.size() ; mm++){
if(xxx[mm] == kx && yyy[mm] == ky)
break;
}
if(mm == xxx.size()){
xxx.push_back(kx);
yyy.push_back(ky);
xx.push_back(kx);
yy.push_back(ky);
}
}
}
}
for(int j = 0 ; j < xxx.size() - 1 ; j++){
for(int k = j+1 ; k < xxx.size() ; k++ ){
if( ( (xxx[j]-xxx[k])*(yyy[j] - y ) == (yyy[j] - yyy[k])*(xxx[j] - x) ) && (( x-xxx[j] )*(x-xxx[k]) > 0) ){
if( (xxx[j] - x)*(xxx[j] -x) + (yyy[j] - y)*(yyy[j]-y ) >
(xxx[k] - x)*(xxx[k] -x) + (yyy[k] - y)*(yyy[k]-y )
){
xxx.erase(xxx.begin() + j);
yyy.erase(yyy.begin() + j);
}
else{
xxx.erase(xxx.begin() + k);
yyy.erase(yyy.begin() + k);
}
}
}
}
num = xxx.size();
cout<<"Case #"<<i+1<<": ";
cout<<num;
cout<<endl;
}
return 0;
}
| 0 |
C10132 | C10325 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cfloat>
#include <climits>
#include <numeric>
#include <ctime>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
#define sz(c) int((c).size())
#define all(c) (c).begin(), (c).end()
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define FORS(i,a,b,s) for (int i = (a); i < (b); i=i+(s))
#define FORD(i,a,b) for (int i = int(b)-1; i >= (a); i--)
#define FORIT(i,c) for (__typeof__((c).begin()) i = (c).begin(); i != (c).end(); i++)
char cmap[30][30];
int H,W,D;
int startx,starty;
int MX,MY;
int cx,cy,ox,oy,dx,dy;
int gone,maxgonesq;
int light() {
// cout << "light ("<<cx<<","<<cy<<") off ("<<ox<<","<<oy<<")" << endl;
if(gone*gone > maxgonesq)
return 0;
if(dx!=0) { // ox -> MX <or> ox -> 0
int e=(dx>0 ? MX-ox : ox);
int noy = oy+(dy*e);
if(cx == startx && cy == starty && e==MX && noy+oy == MY) {
gone+=e/2;
if(gone*gone<=maxgonesq)
return 1;
else
return 0;
}
if(noy > 0 && noy < MY) {
gone += e;
oy=noy;
if(cmap[cx+dx][cy] == '#') {
if(dy==0) {
gone*=2;
if(gone*gone <= maxgonesq)
return 1;
else
return 0;
}
ox=(dx>0 ? MX : 0),dx=-dx;
}
else
ox=(dx>0 ? 0 : MX),cx+=dx;
return light();
} else if(noy==0 || noy==MY) {
gone += e;
if(cmap[cx+dx][cy+dy] != '#') {
ox=(dx>0 ? 0 : MX),cx+=dx;
oy=(dy>0 ? 0 : MY),cy+=dy;
} else if(cmap[cx+dx][cy] == '#' && cmap[cx][cy+dy] == '#') {
gone*=2;
if(gone*gone <= maxgonesq)
return 1;
else
return 0;
} else if(cmap[cx+dx][cy] == '#' && cmap[cx][cy+dy] != '#') {
ox=(dx>0 ? MX : 0),dx=-dx;
oy=(dy>0 ? 0 : MY),cy+=dy;
} else if(cmap[cx+dx][cy] != '#' && cmap[cx][cy+dy] == '#') {
ox=(dx>0 ? 0 : MX),cx+=dx;
oy=(dy>0 ? MY : 0),dy=-dy;
} else
return 0;
return light();
}
}
if(dy!=0) { // oy -> MY <or> oy -> 0
int e=(dy>0 ? MY-oy : oy);
int nox = ox+(dx*e);
if(cx == startx && cy == starty && e==MY && nox+ox == MX) {
gone+=e/2;
if(gone*gone<=maxgonesq)
return 1;
else
return 0;
}
if(nox > 0 && nox < MX) {
gone += e;
ox=nox;
if(cmap[cx][cy+dy] == '#') {
if(dx==0) {
gone*=2;
if(gone*gone <= maxgonesq)
return 1;
else
return 0;
}
oy=(dy>0 ? MY : 0),dy=-dy;
}
else
oy=(dy>0 ? 0 : MY),cy+=dy;
return light();
}
else
cout << "ERROR!!!!!" << endl, exit(0);
}
cout << "ERROR2!!!!!" << endl, exit(0);
return 0;
}
int gcd(int a, int b) {
return b ? gcd(b,a%b) : a;
}
int main() {
int tc;
cin >> tc;
FOR(ctc,1,tc+1) {
cin >> H >> W >> D;
FOR(y,0,H) FOR(x,0,W){
cin >> cmap[x][y];
if(cmap[x][y] == 'X')
startx = x, starty = y, cmap[x][y]='.';
}
int res=0;
FOR(dX,-D,D+1) FOR(dY,-D,D+1) if((dX || dY) && abs(gcd(dX,dY))==1) {
if(!dX || !dY) {
MX = MY = 2;
maxgonesq = 4*D*D;
} else {
MX = 2*abs(dY);
MY = 2*abs(dX);
maxgonesq = (4ll*(ll)D*(ll)D*(ll)dX*(ll)dX*(ll)dY*(ll)dY)/((ll)dX*(ll)dX+(ll)dY*(ll)dY);
}
dx=dX ? dX/abs(dX) : 0;
dy=dY ? dY/abs(dY) : 0;
cx=startx;
cy=starty;
ox = MX/2;
oy = MY/2;
gone=0;
int nres = light();
if(nres) {
// cout << "dX=" << dX << ", dY=" << dY << " got " << nres << endl;
// cout << "maxgonesq=" << maxgonesq << ", gonesq=" << gone*gone << endl;
}
res+=nres;
}
cout << "Case #" << ctc << ": ";
cout << res << endl;
}
return 0;
}
| 0 |
C10132 | C10023 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #pragma once
#define ArraySize_(a) (sizeof(a)/sizeof(a[0]))
#define recast_ reinterpret_cast
#define stcast_ static_cast
| 0 |
C10132 | C10331 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} |
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
struct pt
{
long x;
long y;
pt(){}
pt(long x_,long y_):x(x_),y(y_){}
long abs2(){return x*x+y*y;}
pt operator+(const pt &p){return pt(x+p.x,y+p.y);}
pt operator-(const pt &p){return pt(x-p.x,y-p.y);}
bool operator==(const pt &p){return x==p.x && y==p.y;}
};
bool samesign(long a, long b)
{
if(a>0 && b>0)return true;
if(a<0 && b<0)return true;
return false;
}
double pow2(double x)
{
return x*x;
}
int sgn(long a)
{
if(a<0)return -1;
if(a>0)return 1;
return 0;
}
pt simplify(pt p)
{
int mx=max(abs(p.x),abs(p.y));
for(int i=mx;i>=2;--i)
{
if(p.x%i==0 && p.y%i==0)
{
p.x/=i;
p.y/=i;
}
}
return p;
}
bool samedir(const pt &a, const pt &b)
{
if(a.x==0 && b.x==0)
return samesign(a.y,b.y);
if(a.y==0 && b.y==0)
return samesign(a.x,b.x);
if(!samesign(a.x,b.x) || !samesign(a.y,b.y))
return false;
return simplify(a) == simplify(b);
}
struct D
{
vector< vector<int> > field;
pt player;
int dist;
void loadfield()
{
field.clear();
int w,h;
cin>>h>>w>>dist;
field.resize(h);
for(int y=0;y<h;++y)
{
field[y].resize(w);
for(int x=0;x<w;++x)
{
char c;
cin>>c;
field[y][x] = (c=='#')?1:0;
if(c=='X')
{
player.x=x;
player.y=y;
}
}
}
}
bool at(pt p)
{
return field[p.y][p.x];
}
void adddir(vector<pt> &ps, pt &p)
{
for(int i=0;i<ps.size();++i)
{
if(samedir(ps[i],p))
{
if(ps[i].abs2()<p.abs2())
ps[i]=p;
return;
}
}
ps.push_back(p);
}
vector<pt> dirs;
void gendirs(int dst)
{
dirs.clear();
int dst2=dst*dst;
for(int x=-dst;x<=dst;++x)
{
for(int y=-dst;y<=dst;++y)
{
if(x==0 && y==0)
continue;
pt p(x,y);
if(p.abs2() > dst2)
continue;
adddir(dirs,p);
}
}
}
vector<pt> gen3res()
{
int dd=8;
int dd2=dd*dd;
vector<pt> res;
for(int x=-dd;x<=dd;++x)
{
for(int y=-dd;y<=dd;++y)
{
if(x==0 && y==0)continue;
if((y&3) == 0 || (y&3) == 3)
{
pt p(x,y);
if(p.abs2()>dd2)
continue;
adddir(res, p);
}
}
}
return res;
}
pt reflect(pt p, pt dir, bool xref, bool yref)
{
if(!xref && !yref)
return dir;
if(xref && yref)
{
pt dn( (dir.x<0)?-1:0, (dir.y<0)?-1:0);
pt dx( -1-dn.x, dn.y);
pt dy( dn.x, -1-dn.y);
bool an=at(p+dn);
bool ax=at(p+dx);
bool ay=at(p+dy);
if(!an)
{
return dir;
}
if(!ax && !ay)
{
return pt(0,0); // destroy
}
if(ax && ay)
{
return pt(-dir.x, -dir.y);// ref both
}
if(ax)
{
return pt(dir.x, -dir.y);//ref y
}
//else
{
return pt(-dir.x, dir.y); // ref x
}
}
else if(xref)
{
if(at(p)||at(p+pt(-1,0)))
{
return pt(-dir.x, dir.y);
}
return dir;
}
else //yref
{
if(at(p)||at(p+pt(0,-1)))
{
return pt(dir.x,-dir.y);
}
return dir;
}
}
bool run(pt dir)
{
pt sdir=simplify(dir);
// enh coord
long xa=max(abs(sdir.y),1l);
long ya=max(abs(sdir.x),1l);
pt cur;
cur.x=(player.x*2+1)*xa;
cur.y=(player.y*2+1)*ya;
long xdiv=xa*2;
long ydiv=ya*2;
pt org=cur;
pt curdir(sgn(dir.x), sgn(dir.y));
long maxstep=max(abs(dir.x*xdiv), abs(dir.y*ydiv));
for(int i=0;i<maxstep;++i)
{
cur=cur+curdir;
if(cur == org)
{
return true;// reached image of player
}
bool xref=cur.x%xdiv==0;
bool yref=cur.y%ydiv==0;
if(xref||yref)
{
curdir = reflect( pt(cur.x/xdiv, cur.y/ydiv), curdir, xref, yref);
if(curdir==pt(0,0))
{
return false;//destroyed
}
}
}
return false;
}
void dbg()
{
cout<<"player"<<endl;
cout<<player.x<<" "<<player.y<<endl;
cout<<"dirs"<<endl;
for(int i=0;i<dirs.size();++i)
{
cout<<dirs[i].x<<" "<<dirs[i].y<<endl;
}
}
int solve()
{
loadfield();
gendirs(dist);
int img=0;
for(int i=0;i<dirs.size();++i)
{
bool ok = run(dirs[i]);
if(ok)
{
++img;
}
}
return img;
}
};
int main()
{
int n;
cin>>n;
for(int i=0;i<n;++i)
{
cout<<"Case #"<<i+1<<": "<<D().solve()<<endl;
}
return 0;
}
| 0 |
C10132 | C10249 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <string>
using namespace std;
#include <assert.h>
const unsigned maxT = 100;
const unsigned maxHW = 100;
const unsigned maxD = 50;
unsigned H, W, D;
unsigned myH, myW;
char map[maxHW][maxHW];
unsigned gcd(int m, int n) {
assert(m > 0 && n > 0);
if (m > n) {
int t = m;
m = n;
n = t;
}
while (m > 1) {
int rem = n % m;
if (rem == 0) {
return m;
}
n = m;
m = rem;
}
return 1;
}
unsigned straight(int dh, int dw) {
unsigned dots = 0;
int h = myH;
int w = myW;
for (;;) {
h += dh;
w += dw;
if (map[h][w] == '.') {
dots++;
} else {
break;
}
}
if (2 * dots + 1 <= D) {
return 1;
} else {
return 0;
}
}
int test(int hcoord, int wcoord, int dh, int dw, int mult) {
int odh = dh; int odw = dw;
int hgrid = 2 * wcoord;
int wgrid = 2 * hcoord;
int dist = 2 * hcoord * wcoord * mult;
int hstart = myH * hgrid + wcoord;
int wstart = myW * wgrid + hcoord;
int h = hstart;
int w = wstart;
for (; dist > 0; dist--) {
int hcell = h;
if (dh < 0) {
hcell = h - 1;
}
int wcell = w;
if (dw < 0) {
wcell = w - 1;
}
char newtype = map[hcell/hgrid][wcell/wgrid];
if (newtype == '#') {
int hhcell = h;
if (dh > 0) {
hhcell = h - 1;
}
int wwcell = w;
if (dw > 0) {
wwcell = w - 1;
}
char htype = map[hhcell/hgrid][wcell/wgrid];
char wtype = map[hcell/hgrid][wwcell/wgrid];
if (htype == '#') {
dw = -dw;
if (wtype == '#') {
dh = -dh;
}
} else if (wtype == '#') {
dh = -dh;
} else {
return 0;
}
}
h += dh;
w += dw;
if (h == hstart && w == wstart) {
return 1;
}
}
return 0;
}
void doCase() {
cin >> H >> W >> D;
assert(H >= 3 && H <= maxHW);
assert(W >= 3 && W <= maxHW);
assert(D >= 1 && D <= maxD);
myH = maxHW;
myW = maxHW;
string line;
for (unsigned h = 0; h < H; h++) {
cin >> line;
assert(line.length() == W);
for (unsigned w = 0; w < W; w++) {
char c = line[w];
assert(c == '.' || c == '#' || c == 'X');
map[h][w] = c;
if (c == 'X') {
assert(myH == maxHW);
myH = h;
myW = w;
}
}
}
assert(myH < maxHW);
for (unsigned h = 0; h < H; h++) {
assert(map[h][0] == '#' && map[h][W-1] == '#');
}
for (unsigned w = 0; w < W; w++) {
assert(map[0][w] == '#' && map[H-1][w] == '#');
}
unsigned images = 0;
images += straight(0, 1);
images += straight(0, -1);
images += straight(1, 0);
images += straight(-1, 0);
for (int h = 1; h < D; h++) {
for (int w = 1; h*h + w*w <= D*D; w++) {
if (gcd(h, w) == 1) {
int mult = 1;
while ((mult+1)*(mult+1)*(h*h + w*w) <= D*D) {
mult++;
}
images += test(h, w, 1, 1, mult);
images += test(h, w, 1, -1, mult);
images += test(h, w, -1, 1, mult);
images += test(h, w, -1, -1, mult);
}
}
}
cout << images << endl;
}
int main() {
unsigned T;
cin >> T;
assert(T >= 1 && T <= maxT);
for (unsigned t = 1; t <= T; t++) {
cout << "Case #" << t << ": ";
doCase();
}
return 0;
}
| 0 |
C10132 | C10245 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | /*
* main.cpp
*
* Created on: 2011/09/24
* Author: taik
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <vector>
#include <set>
#include <list>
#include <algorithm>
typedef long long s64;
#define debug_printf printf
//#define debug_printf
struct Mirror{
//public:
double Normal[2]; // æ³ç·æ¹å
double Start[2]; // å§ç¹
double End[2]; // çµç¹
};
static double my_abs(double a){
return a > 0 ? a : -a;
}
static void mirrorPoint(double *target,const double *source,const double *base,bool x_axis){
int fix = 1;
int rev = 0;
if (x_axis){ // X 座æ¨ã¯å¤ãããªã
fix = 0;
rev = 1;
}
target[fix] = source[fix];
target[rev] = source[rev] - (source[rev] - base[rev]) * 2;
}
class MirrorWorld{
private:
Mirror *mirrors_;
int mirror_count_;
double me_[2]; // Me;
double base_[2]; // å·¦ä¸ç¹
int parent_mirror_; // ãã®ä¸çãçã¿åºããé¡.
MirrorWorld(){}
public:
MirrorWorld(double meX,double meY,Mirror *mirrors,int mirror_count){
base_[0] = base_[1] = 0.0;
me_[0] = meX;
me_[1] = meY;
mirrors_ = new Mirror[mirror_count];
mirror_count_ = mirror_count;
for(int i=0;i<mirror_count_;++i){
for(int j=0;j<2;++j){
// printf("mi %lf,%lf\n",mirrors[i].End[0],mirrors[i].End[1]);
mirrors_[i].Start[j] = mirrors[i].Start[j];
mirrors_[i].End[j] = mirrors[i].End[j];
mirrors_[i].Normal[j] = mirrors[i].Normal[j];
}
}
}
void Print(){
printf("base %lf,%lf\n",base_[0],base_[1]);
printf("me %lf,%lf\n",me_[0],me_[1]);
for(int i=0;i<mirror_count_;++i){
printf("mirror %d n(%lf,%lf),s(%lf,%lf),e(%lf,%lf),\n",
i,
mirrors_[i].Normal[0],mirrors_[i].Normal[1],
mirrors_[i].Start[0],mirrors_[i].Start[1],
mirrors_[i].End[0],mirrors_[i].End[1]);
}
}
~MirrorWorld(){
delete[] mirrors_;
}
int GetMirrorCount(){
return mirror_count_;
}
Mirror *GetMirror(int mirror_index){
return &mirrors_[mirror_index];
}
double *GetMe(){
return me_;
}
int GetParentMirror(){
return parent_mirror_;
}
MirrorWorld *CreateMirrorWorld(int target_mirror_ind){
MirrorWorld *new_world = new MirrorWorld();
new_world->parent_mirror_ = target_mirror_ind;
Mirror *target_mirror = &mirrors_[target_mirror_ind];
bool x_axis = false;
if (my_abs(target_mirror->Normal[0]) < my_abs(target_mirror->Normal[1])){ // X 座æ¨ã¯å¤ãããªã
x_axis = true;
}
mirrorPoint(new_world->me_,me_,target_mirror->Start,x_axis);
mirrorPoint(new_world->base_,base_,target_mirror->Start,x_axis);
new_world->mirror_count_ = mirror_count_;
new_world->mirrors_ = new Mirror[mirror_count_];
for(int i=0;i<mirror_count_;++i){
mirrorPoint(new_world->mirrors_[i].Start,mirrors_[i].Start,target_mirror->Start,x_axis);
mirrorPoint(new_world->mirrors_[i].End,mirrors_[i].End,target_mirror->Start,x_axis);
new_world->mirrors_[i].Normal[0] = mirrors_[i].Normal[0];
new_world->mirrors_[i].Normal[1] = mirrors_[i].Normal[1];
if (!x_axis){
new_world->mirrors_[i].Normal[0] *= -1;
}
else{
new_world->mirrors_[i].Normal[1] *= -1;
}
}
return new_world;
}
};
static double getDot(double *a,double *b){
return a[0] * b[0] + a[1] * b[1];
}
static double* getSub(double *ab,double *a,double *b){
ab[0] = a[0] - b[0];
ab[1] = a[1] - b[1];
return ab;
}
static double getLen(double *a){
return sqrt(a[0] * a[0] + a[1] * a[1]);
}
static double getCross(double *a,double *b){
return a[0] * b[1] - a[1] * b[0];
}
static bool isLinear(double *a,double *b,double *c){
double ba[2];
double ca[2];
getSub(ba,b,a);
getSub(ca,c,a);
if ((ba[0] * ca[0] < 0) || (ba[1] * ca[1] < 0)){
return false; // 符å·éã.
}
if (((my_abs(ba[0]) <= DBL_EPSILON) && (my_abs(ba[1]) <= DBL_EPSILON))
|| ((my_abs(ca[0]) <= DBL_EPSILON) && (my_abs(ca[1]) <= DBL_EPSILON))){ // åä¸ç¹
return true;
}
if (my_abs(getCross(ba,ca)) <= DBL_EPSILON){
return true;
}
return false;
}
static bool isCrossed(double *a1,double *a2,double *b1,double *b2,bool on_point){
double a2a1[2];
double b1a1[2];
double b2a1[2];
double b2b1[2];
double a1b1[2];
double a2b1[2];
getSub(a2a1,a2,a1);
getSub(b1a1,b1,a1);
getSub(b2a1,b2,a1);
getSub(b2b1,b2,b1);
getSub(a1b1,a1,b1);
getSub(a2b1,a2,b1);
double threshold = 0;
if (on_point){
threshold = DBL_EPSILON;
}
return ( getCross(a2a1, b1a1) * getCross(a2a1, b2a1) < threshold ) &&
( getCross(b2b1, a1b1) * getCross(b2b1, a2b1) < threshold );
}
static double getT(double *p,double *e,double *s){
double xdiff = (e[0] - s[0]);
if (my_abs(xdiff) > DBL_EPSILON){
return (p[0] - s[0]) / xdiff;
}
else{
return (p[1] - s[1]) / (e[1] - s[1]);
}
}
static bool getCrossPoint(double *ret,double *a1,double *a2,double *b1,double *b2){
double a2a1[2];
double b2b1[2];
double b1a1[2];
getSub(a2a1,a2,a1);
getSub(b1a1,b1,a1);
getSub(b2b1,b2,b1);
double crs = getCross(b2b1, a2a1);
if (my_abs(crs) <= DBL_EPSILON){ // å¹³è¡.
// printf("parallel\n");
return false;
}
double scale = getCross(b2b1,b1a1) / crs;
for(int i=0;i<2;++i){
ret[i] = a1[i] + a2a1[i] * scale;
}
return true;
}
static double getDistancePL(double *a,double *b,double *c){
double ba[2];
double ca[2];
double ab[2];
double cb[2];
if ( getDot(getSub(ba,b,a),getSub(ca,c,a)) < 0.0 ) return getLen(getSub(ca,c,a));
if ( getDot(getSub(ab,a,b),getSub(cb,c,b)) < 0.0 ) return getLen(getSub(cb,c,b));
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
return my_abs(getCross(getSub(ba,b,a),getSub(ca,c,a))) / getLen(getSub(ba,b,a));
}
static double clamp(double x, double min, double max){
if (x < min) return min;
if (x > max) return max;
return x;
}
static void q4(MirrorWorld **mirror_worlds,int target_mirror,
double *base,
double left_x,double left_y,
double right_x,double right_y,
double d,std::set<std::pair<double,double> > *result){
MirrorWorld *mw = mirror_worlds[target_mirror];
//mw->Print();
//getchar();
// debug_printf("target %d\n",target_mirror);
// ç¹å¤å®
// èªåã¨ã®è·é¢ã測ã.
if (target_mirror > 0){
double tmp[2];
if (getLen(getSub(tmp,base,mw->GetMe())) -d <= DBL_EPSILON){// æ¡ä»¶ 1 åã¾ã§ã®è·é¢ã d 以ä¸
bool pass = true;
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
for(std::set<std::pair<double,double> >::iterator itr = result->begin();
itr != result->end();itr++){ // æ¡ä»¶ 2 åä¸ç´ç·ç¶ã®è§£ã¯ãªã.
double a[2];
a[0] = itr->first;
a[1] = itr->second;
if (isLinear(base,a,mw->GetMe())){
pass = false;
break;
}
}
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
if (pass){
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
// æ¡ä»¶ 3 çæãããé¡é¢ããã¹ã¦éã.ãã以å¤ã®é¡é¢ãéããªã.
for(int i=1;i<=target_mirror;++i){
int ignore = -1;
MirrorWorld *tmp_mw = mirror_worlds[i];
if (i < target_mirror){// 次ã®ãã¤ã¯å¤å®ããã¯ããã¦ãã
MirrorWorld *tmp_mw_next = mirror_worlds[i+1];
ignore = tmp_mw_next->GetParentMirror();
}
for(int j=0;j<tmp_mw->GetMirrorCount();++j){
if (j == ignore){
continue;
}
Mirror *m = tmp_mw->GetMirror(j);
if (j == tmp_mw->GetParentMirror()){
if (!isCrossed(m->Start,m->End,mw->GetMe(),base,true)){
pass = false;
break;
}
}
else{
if (isCrossed(m->Start,m->End,mw->GetMe(),base,false)){
pass = false;
break;
}
}
}
if (!pass){
break;
}
}
}
if (pass){ // OK.
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
// for(int i=1;i<=target_mirror;++i){
// debug_printf("->%d",mirror_worlds[i]->GetParentMirror());
// }
// debug_printf("\n");
result->insert(std::pair<double,double>(mw->GetMe()[0],mw->GetMe()[1]));
}
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
}
}
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
// é¡é¢çæ
for(int i=0;i<mw->GetMirrorCount();++i){
Mirror *m = mw->GetMirror(i);
double tmp[2];
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
if (getDot(m->Normal,getSub(tmp,m->Start,base)) < 0.0){ // æ¡ä»¶ 1 æ³ç·ã®åããæ£ãã
double dist = getDistancePL(m->Start,m->End,base);
// debug_printf("DB dist : %lf\n",dist);
if (dist < d){// æ¡ä»¶ 2 é¡é¢ã¾ã§ã®è·é¢ãd ããå°ãã
// è¦éè§ã®ç«¯ã¨é¡é¢ã¨ã®äº¤ç¹ãæ±ãã.
double left_hit[2];
double right_hit[2];
double lt = 0.0;
double rt = 1.0;
double ls = 2.0;
double rs = 2.0;
if (target_mirror > 0){ // æåã¯å¿
ã 0,1
double left[2]; left[0] = left_x;left[1] = left_y;
double right[2]; right[0] = right_x;right[1] = right_y;
// debug_printf("l,r %lf,%lf,%lf,%lf\n",left[0],left[1],right[0],right[1]);
getCrossPoint(left_hit,base,left,m->Start,m->End);
getCrossPoint(right_hit,base,right,m->Start,m->End);
// debug_printf("lh,rh %lf,%lf,%lf,%lf\n",left_hit[0],left_hit[1],right_hit[0],right_hit[1]);
// åªä»å¤æ°ãæ±ãã.
lt = getT(left_hit,m->End,m->Start);
rt = getT(right_hit,m->End,m->Start);
ls = getT(left_hit,left,base);
rs = getT(right_hit,right,base);
// debug_printf("lt,rt %lf,%lf\n",lt,rt);
if (!((ls < 0 ) && (rs < 0))){
if ( ls < 0){
if (lt > rt){
if (rt < 0) lt = rt;
else lt = 0.0;
}
else{
if (rt > 1.0) lt = rt;
else lt = 1.0;
}
ls = 2.0;
}
if ( rs < 0){
if (rt > lt){
if (lt < 0) rt = lt;
else rt = 0.0;
}
else{
if (lt > 1.0) rt = lt;
else rt = 1.0;
}
rs = 2.0;
}
}
}
if (((lt > 1.0) && (rt > 1.0)) || ((lt < 0.0) && (rt < 0.0)) || ((ls <= 1.0) && (rs <= 1.0)) || ((ls < 1.0) && (rs < 1.0))){ // è¦éç¯å²å¤ãªã®ã§é¡é¢ãä½æããªã.
// debug_printf("rejected"
// "lt=%lf,rt=%lf,ls=%lf,rs=%lf\n",lt,rt,ls,rs);
}
else{
lt = clamp(lt,0.0,1.0);
rt = clamp(rt,0.0,1.0);
// æ°ãã交ç¹ãæ±ãã.
for(int tmp=0;tmp<2;++tmp){
left_hit[tmp] = m->Start[tmp] + (m->End[tmp] - m->Start[tmp]) * lt;
right_hit[tmp] = m->Start[tmp] + (m->End[tmp] - m->Start[tmp]) * rt;
}
// debug_printf("next l,r %lf,%lf,%lf,%lf\n",left_hit[0],left_hit[1],right_hit[0],right_hit[1]);
MirrorWorld *new_world = mw->CreateMirrorWorld(i);
// debug_printf("created mirror world %d\n",target_mirror + 1);
mirror_worlds[target_mirror + 1] = new_world;
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
q4(mirror_worlds,target_mirror + 1,base,
left_hit[0],left_hit[1],
right_hit[0],right_hit[1],
d,result);
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
delete new_world;
}
}
}
}
// debug_printf("DB %s:%d\n",__FUNCTION__,__LINE__);
}
static void addNewMirror(std::vector<Mirror> *l, Mirror *m){
int erase = -1;
for(int i=0;i<l->size();++i){
if ((l->at(i).Start[0] == m->Start[0]) &&
(l->at(i).Start[1] == m->Start[1]) &&
(l->at(i).End[0] == m->End[0]) &&
(l->at(i).End[1] == m->End[1])){
erase = i;
break;
}
else if ((l->at(i).Start[0] == m->End[0]) &&
(l->at(i).Start[1] == m->End[1]) &&
(l->at(i).End[0] == m->Start[0]) &&
(l->at(i).End[1] == m->Start[1])){
erase = i;
break;
}
}
if (erase>=0){
for(int i=erase;i<l->size()-1;++i){
(*l)[i] = (*l)[i+1];
}
l->pop_back();
}
else{
l->push_back(*m);
}
}
static void mergeMirror(std::vector<Mirror> *mirrors){
std::list<Mirror> result;
std::set<int> removed;
for(int i=0;i < mirrors->size();i++){
if (removed.find(i) != removed.end()){
continue;
}
Mirror m = (*mirrors)[i];
for(int j=i+1;j<mirrors->size();++j){
if (removed.find(j) != removed.end()){
continue;
}
Mirror he = (*mirrors)[j];
bool rem = false;
if ((m.Normal[0] == he.Normal[0]) && (m.Normal[1] == he.Normal[1])){
if ((m.End[0] == he.Start[0]) && (m.End[1] == he.Start[1])){
m.End[0] = he.End[0];
m.End[1] = he.End[1];
rem = true;
}
else if ((m.End[0] == he.End[0]) && (m.End[1] == he.End[1])){
m.End[0] = he.Start[0];
m.End[1] = he.Start[1];
rem = true;
}
else if ((m.Start[0] == he.End[0]) && (m.Start[1] == he.End[1])){
m.Start[0] = he.Start[0];
m.Start[1] = he.Start[1];
rem = true;
}
else if ((m.Start[0] == he.Start[0]) && (m.Start[1] == he.Start[1])){
m.Start[0] = he.End[0];
m.Start[1] = he.End[1];
rem = true;
}
}
if (rem){
removed.insert(j);
}
}
result.push_back(m);
}
mirrors->clear();
for(std::list<Mirror>::iterator itr=result.begin();itr!=result.end();++itr){
mirrors->push_back(*itr);
}
}
int main(int argc,const char *argv[]){
if (argc < 2){
printf("Usage : %s input\n",argv[0]);
return 0;
}
FILE *file = fopen(argv[1],"r");
char line[65536];
const char *delim = " ";
if(fgets(line,sizeof(line),file) == NULL){
return 0;
}
int tc = atoi(line); // test case
for(int i=0;i<tc;++i){
printf("Case #%d: ",i+1);
fgets(line,sizeof(line),file);
char *cp = line;
int h = atoi(strtok(cp,delim));
cp = NULL;
int w = atoi(strtok(cp,delim));
double d = (double)atoi(strtok(cp,delim));
int ans = 0;
std::vector<Mirror > mirrors;
// æå¤å¨ã®ãã©ã¼ãä½æ.
for(int j=0;j<w-2;++j){ // ä¸
Mirror m;
m.Normal[0] = 0.0;m.Normal[1] = -1.0;
m.Start[0] = (double)j; m.Start[1] = (double)(h-2);
m.End[0] = (double)(j+1);m.End[1] = (double)(h-2);
mirrors.push_back(m);
}
for(int j=h-2;j>0;--j){ // å³
Mirror m;
m.Normal[0] = -1.0;m.Normal[1] = 0.0;
m.Start[0] = (double)(w-2); m.Start[1] = (double)(j);
m.End[0] = (double)(w-2); m.End[1] = (double)(j-1);
mirrors.push_back(m);
}
for(int j=w-2;j>0;--j){ // ä¸
Mirror m;
m.Normal[0] = 0.0;m.Normal[1] = 1.0;
m.Start[0] = (double)j; m.Start[1] = 0.0;
m.End[0] = (double)(j-1);m.End[1] = 0.0;
mirrors.push_back(m);
}
for(int j=0;j<h-2;++j){ // å·¦
Mirror m;
m.Normal[0] = 1.0;m.Normal[1] = 0.0;
m.Start[0] = 0.0; m.Start[1] = (double)(j);
m.End[0] = 0.0;m.End[1] = (double)(j+1);
mirrors.push_back(m);
}
double base[2];
for(int j=h-1;j>=0;--j){
fgets(line,sizeof(line),file);
if ((j== h-1) || (j==0)){// æå¤å¨ã® #
continue;
}
for(int k=1;k<w-1;++k){
if (line[k] == 'X'){
base[0] = ((double)k)-0.5;
base[1] = ((double)j)-0.5;
}
if (line[k] == '#'){
{ // ä¸
Mirror m;
m.Normal[0] = 0.0;m.Normal[1] = 1.0;
m.Start[0] = (double)(k-1); m.Start[1] = (double)(j);
m.End[0] = (double)(k);m.End[1] = (double)(j);
addNewMirror(&mirrors,&m);
}
{ // å³
Mirror m;
m.Normal[0] = 1.0;m.Normal[1] = 0.0;
m.Start[0] = (double)(k); m.Start[1] = (double)(j);
m.End[0] = (double)(k); m.End[1] = (double)(j-1);
addNewMirror(&mirrors,&m);
}
{ // ä¸
Mirror m;
m.Normal[0] = 0.0;m.Normal[1] = -1.0;
m.Start[0] = (double)k; m.Start[1] = (double)(j-1);
m.End[0] = (double)(k-1);m.End[1] = (double)(j-1);
addNewMirror(&mirrors,&m);
}
{ // å·¦
Mirror m;
m.Normal[0] = -1.0;m.Normal[1] = 0.0;
m.Start[0] = (double)(k-1); m.Start[1] = (double)(j-1);
m.End[0] = (double)(k-1); m.End[1] = (double)(j);
addNewMirror(&mirrors,&m);
}
}
}
}
/*
for(int j=0;j<h;++j){
{{0.0,-1.0},{0.0,4.0},{4.0,4.0}},
{{-1.0,0.0},{4.0,4.0},{4.0,0.0}},
{{0.0,1.0},{0.0,0.0},{4.0,0.0}},
{{1.0,0.0},{0.0,4.0},{0.0,0.0}},
{{0.0,1.0},{4.0,2.0},{5.0,2.0}},
// {{1.0,0.0},{5.0,2.0},{5.0,1.0}},
{{-1.0,0.0},{5.0,1.0},{4.0,1.0}},
{{0.0,-1.0},{4.0,1.0},{4.0,2.0}},
};
*/
mergeMirror(&mirrors);
Mirror *ms = (Mirror *)malloc(sizeof(Mirror)*mirrors.size());
for(int j=0;j<mirrors.size();++j){
ms[j] = mirrors[j];
}
MirrorWorld orig(base[0],base[1],ms,mirrors.size());
free(ms);
// orig.Print();
// MirrorWorld *mw = orig.CreateMirrorWorld(&ms[0]);
// mw->Print();
MirrorWorld **mirror_worlds = (MirrorWorld **)malloc(sizeof(MirrorWorld * ) * 65535);
mirror_worlds[0] = &orig;
std::set<std::pair<double,double> > result;
result.clear();
q4(mirror_worlds,0,base,0,0,0,0,d ,&result);
printf("%d\n",result.size());
}
fclose(file);
return 0;
}
| 0 |
C10132 | C10040 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return n & two(b); }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
int R, C, D, sr, sc;
char board[2][31][31];
int gcd(int a, int b)
{
if (!b) return a;
return gcd(b, a%b);
}
bool Simulate(int sr, int sc, int dr, int dc, int mr, int mc, char board[31][31]) // dr <= dc
{
int steps = 1;
dr *= 2;
dc *= 2;
int r = sr, c = sc, ar = dc/2;
while (steps*steps*(dr*dr+dc*dc)/4 <= D*D)
{
REP(step, dc/2)
{
int aar = (2*dc-(ar+dr))%dc, rr = r+(ar+dr)/dc*mr, cc = c+mc;
if (ar+dr < dc) // no row shift
{
if (board[rr][cc] == '#')
mc = -mc;
else c = cc;
}
else if (ar == aar) // through the corner
{
if (board[rr][cc] == '#')
{
if (board[rr-mr][cc] == '#' && board[rr][cc-mc] == '#')
{
mr = -mr;
mc = -mc;
}
else if (board[rr-mr][cc] == '#')
{
mc = -mc;
r = rr;
}
else if (board[rr][cc-mc] == '#')
{
mr = -mr;
c = cc;
}
else return false;
}
else
{
r = rr;
c = cc;
}
}
else if (ar < aar) // row first
{
if (board[rr][c] == '#')
mr = -mr;
else r = rr;
if (board[r][cc] == '#')
mc = -mc;
else c = cc;
}
else // col first
{
if (board[r][cc] == '#')
mc = -mc;
else c = cc;
if (board[rr][c] == '#')
mr = -mr;
else r = rr;
}
ar = (ar+dr)%dc;
}
if (r == sr && c == sc)
return true;
++steps;
}
return false;
}
void Solve(int tc)
{
scanf("%d%d%d", &R, &C, &D);
REP(i, R)
{
scanf("%s", board[0][i]);
REP(j, C)
{
if (board[0][i][j] == 'X')
{
sr = i;
sc = j;
board[0][i][j] = '.';
}
board[1][j][i] = board[0][i][j];
}
}
int res = 0;
FOR(dc, 1, D) FOR(dr, 0, dc)
if (gcd(dr, dc) == 1)
{
res += Simulate(sr, sc, dr, dc, 1, 1, board[0]);
res += Simulate(sr, sc, dr, dc, 1, -1, board[0]);
if (dr)
{
res += Simulate(sr, sc, dr, dc, -1, 1, board[0]);
res += Simulate(sr, sc, dr, dc, -1, -1, board[0]);
}
if (dr < dc)
{
res += Simulate(sc, sr, dr, dc, 1, 1, board[1]);
res += Simulate(sc, sr, dr, dc, 1, -1, board[1]);
if (dr)
{
res += Simulate(sc, sr, dr, dc, -1, 1, board[1]);
res += Simulate(sc, sr, dr, dc, -1, -1, board[1]);
}
}
}
printf("Case #%d: %d\n", tc, res);
}
int main()
{
int T;
scanf("%d\n", &T);
FOR(tc,1,T) Solve(tc);
return 0;
}
/*
1
3 10 20
##########
#X.......#
##########
*/ | 0 |
C10132 | C10082 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define SZ(x) ((int) (x).size())
#define maxit(x,y) ((x) = max((x),(y)))
#define minit(x,y) ((x) = min((x),(y)))
typedef long long LL;
bool was[4][2000][2000];
bool wasat(int dir, int dr, int dc)
{
if (dr >= 2000 || dc >= 2000) return true;
int g = __gcd(dr, dc);
dr /= g;
dc /= g;
return was[dir][dr][dc];
}
void setat(int dir, int dr, int dc)
{
if (dr >= 2000 || dc >= 2000) return;
int g = __gcd(dr, dc);
dr /= g;
dc /= g;
was[dir][dr][dc] = true;
}
int main()
{
setlinebuf(stdout);
int cases;
scanf("%d", &cases);
for (int T = 1; T <= cases; ++T) {
memset(was, 0, sizeof was);
int R, C, D;
int rr = -1, cc = -1;
scanf("%d %d %d", &R, &C, &D);
for (int r = 0; r < R; ++r) {
char buf[256];
scanf("%s", buf);
char *p = strchr(buf, 'X');
if (p != NULL) {
rr = r;
cc = p - buf;
}
}
long r1 = 1 + 2*(rr - 1);
long r2 = 1 + 2*(R-2 - rr);
long c1 = 1 + 2*(cc - 1);
long c2 = 1 + 2*(C-2 - cc);
int result = 0;
for (int r = 0; r < 1600; ++r) {
for (int c = 0; c < 1600; ++c) {
if (r == 0 && c == 0) continue;
long dr1 = (r-1)*2*(R-2) + r1 + ((1-r%2)?r2:r1);
long dr2 = (r-1)*2*(R-2) + r2 + ((1-r%2)?r1:r2);
if (r == 0) dr1 = dr2 = 0;
long dc1 = (c-1)*2*(C-2) + c1 + ((1-c%2)?c2:c1);
long dc2 = (c-1)*2*(C-2) + c2 + ((1-c%2)?c1:c2);
if (c == 0) dc1 = dc2 = 0;
int cnt = 0;
if (r == 0) {
if (!wasat(0, dr1, dc1) && dc1*dc1 <= 4*D*D) cnt += 1;
setat(0, dr1, dc1);
if (!wasat(1, dr1, dc2) && dc2*dc2 <= 4*D*D) cnt += 1;
setat(1, dr1, dc2);
} else if (c == 0) {
if (!wasat(2, dr1, dc1) && dr1*dr1 <= 4*D*D) cnt += 1;
setat(2, dr1, dc1);
if (!wasat(3, dr2, dc1) && dr2*dr2 <= 4*D*D) cnt += 1;
setat(3, dr2, dc1);
} else {
if (!wasat(0, dr1, dc1) && dr1*dr1 + dc1*dc1 <= 4*D*D) cnt += 1;
setat(0, dr1, dc1);
if (!wasat(1, dr2, dc1) && dr2*dr2 + dc1*dc1 <= 4*D*D) cnt += 1;
setat(1, dr2, dc1);
if (!wasat(2, dr1, dc2) && dr1*dr1 + dc2*dc2 <= 4*D*D) cnt += 1;
setat(2, dr1, dc2);
if (!wasat(3, dr2, dc2) && dr2*dr2 + dc2*dc2 <= 4*D*D) cnt += 1;
setat(3, dr2, dc2);
}
//if (cnt != 0) printf("%d %d => %d\n", r, c, cnt);
result += cnt;
}
}
printf("Case #%d: %d\n", T, result);
}
return 0;
}
| 0 |
C10132 | C10212 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned short us;
typedef unsigned char uc;
int gcd(int x,int y){int t;if(!x)return y;for(y%=x;y;t=y,y=x%y,x=t);return x;}
main()
{
int cases;
cin >> cases;
for(int loop=1; loop<=cases; loop++)
{
int h,w,d;
printf("Case #%d: ",loop);
cin >> h >> w >> d;
set<pair<int, int> > S;
char M[50][50];
for(int i=0;i<h;i++) scanf("%s",M[i]);
int r, c;
for(int i=0;i<h;i++) for(int j=0;j<w;j++) if (M[i][j]=='X') {r=i;c=j;}
r--; c--; h-=2; w-=2;
//printf("(%d,%d) in %dx%d\n",r,c,h,w);
int R = 55;
for(int x=-R;x<=R;x++) for(int y=-R;y<=R;y++)
{
int dc,dr;
if (x==0&&y==0) continue;
if (x%2==0) { dc = w*x; } else { dc = w*x + 2*c + w+1; }
if (y%2==0) { dr = h*y; } else { dr = h*y + 2*r + h+1; }
int d2 = dr*dr+dc*dc;
if (d2>0 && d2<=d*d)
{
//printf("Got <%d,%d>\n",dr,dc);
int g=gcd(abs(dr),abs(dc)); dr /= g; dc /= g;
S.insert(make_pair(dr,dc));
//cout << S.size() << endl;
}
}
cout << S.size() << endl;
}
}
| 0 |
C10132 | C10058 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include <stdio.h>
#include <map>
using namespace std;
int h, w, d;
char in[50][50];
int s[4][2], x, y;
map<int, map<int, int> > mark;
int gcd(int x, int y) {
int c;
if(x == 0) return y;
if(y == 0) return x;
while(c = x % y) {
x = y;
y = c;
}
return y;
}
int abs(int x) {
if(x < 0) x = -x;
return x;
}
int isOK() {
int a = x - s[0][0], b = y - s[0][1];
if(a == 0 && b == 0) return 0;
int dis = a * a + b * b;
if(dis <= 4 * d * d) {
int c = gcd(abs(a), abs(b));
a /= c;
b /= c;
if(mark[a][b] == 0) {
//printf("%d %d %d\n", x, y, dis);
mark[a][b] = 1;
return 1;
} else {
return 0;
}
}
return 0;
}
int f() {
int ret = 0;
for(int i = -50; i <= 50; ++i) {
for(int j = -50; j <= 50; ++j) {
for(int l = 0; l < 4; ++l) {
x = s[l][0] + i * (h - 2) * 4;
y = s[l][1] + j * (w - 2) * 4;
ret += isOK();
}
}
}
return ret;
}
int solve() {
for(int i = 0; i < h; ++i) {
for(int j = 0; j < w; ++j) {
if(in[i][j] == 'X') {
s[0][0] = i * 2 - 1;
s[0][1] = j * 2 - 1;
s[1][0] = -s[0][0];
s[1][1] = s[0][1];
s[2][0] = s[0][0];
s[2][1] = -s[0][1];
s[3][0] = -s[0][0];
s[3][1] = -s[0][1];
mark.clear();
return f();
}
}
}
return 0;
}
int main() {
int t;
freopen("D-small-attempt1.in", "r", stdin);
freopen("out.txt", "w", stdout);
scanf("%d", &t);
for(int i = 0; i < t; ++i) {
scanf("%d %d %d", &h, &w, &d);
gets(in[0]);
for(int i = 0; i < h; ++i) {
gets(in[i]);
}
printf("Case #%d: %d\n", i + 1, solve());
}
scanf("%d");
return 0;
}
| 0 |
C10132 | C10282 | #include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
char a[1 << 5][1 << 5];
const int dx[4] = {-1, -1, 1, 1};
const int dy[4] = {-1, 1, 1, -1};
int h, w, d, t;
int sx, sy;
set<pair<int, int> > S;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x % y);
}
pair<int, int> N(int x, int y)
{
if (x == 0)
return make_pair(0, y / abs(y));
if (y == 0)
return make_pair(x / abs(x), 0);
int d = gcd(abs(x), abs(y));
return make_pair(x / d, y / d);
}
int len(int x, int y)
{
return x * x + y * y;
}
int main()
{
freopen("D.in", "r", stdin);
freopen("output.txt", "w", stdout);
scanf("%d", &t);
for(int test = 1; test <= t; ++test)
{
S.clear();
scanf("%d%d%d", &h, &w, &d);
for(int i = 0; i < h; ++i)
scanf("%s", a[i]);
for(int i = 0; i < h; ++i)
for(int j = 0; j < w; ++j)
if (a[i][j] == 'X')
sx = 2 * (j - 1) + 1, sy = 2 * (i - 1) + 1;
w = (w - 2) * 2, h = (h - 2) * 2;
int x, y;
for(int i = -d; i <= d; ++i)
{
for(int j = -d; j <= d; ++j)
{
for(int k = 0; k < 4; ++k)
{
x = dx[k] * sx + 2 * w * i - sx;
y = dy[k] * sy + 2 * h * j - sy;
if (x == 0 && y == 0)
continue;
if (len(x, y) <= 4 * d * d)
S.insert(N(x, y));
}
}
}
printf("Case #%d: %d\n", test, (int)S.size());
}
return 0;
} | #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
const double eps = 1e-7;
int n, m, k, sx, sy;
char s[35][35];
bool u[105][105];
int gcd(int a, int b) {
if(b==0) return a;
else return gcd(b, a%b);
}
double get(double x, double d) {
if(d > eps) {
double res = (int)(x+1) - x;
while(res > 1) res -= 1;
while(res < eps) res += 1;
return res / d;
} else if(d < -eps) {
double res = x - (int)x;
while(res > 1) res -= 1;
while(res < eps) res += 1;
return res / -d;
} else {
return 1.0;
}
}
double SX, SY;
bool go(double sx, double sy, double dx, double dy) {
double t = min(1.0, min(get(sx, dx), get(sy, dy)));
sx += dx * t;
sy += dy * t;
dx -= dx * t;
dy -= dy * t;
if(fabs(t-1) < eps) {
return fabs(sx - SX) < eps && fabs(sy - SY) < eps;
}
int x, y, block=0;
if(fabs(sx - (int)(sx+.5)) < eps) block |= 1;
if(fabs(sy - (int)(sy+.5)) < eps) block |= 2;
if(dx > 0) x = (int)(sx + eps);
else x = (int)(sx - eps);
if(dy > 0) y = (int)(sy + eps);
else y = (int)(sy - eps);
if(s[x][y] != '#') return go(sx, sy, dx, dy);
if(block == 1) return go(sx, sy, -dx, dy);
else if(block == 2) return go(sx, sy, dx, -dy);
else {
int DX = dx > 0 ? -1 : 1;
int DY = dy > 0 ? -1 : 1;
bool px = (s[x+DX][y] == '#');
bool py = (s[x][y+DY] == '#');
if(px && py) {
return go(sx, sy, -dx, -dy);
} else if(px) {
return go(sx, sy, dx, -dy);
} else if(py) {
return go(sx, sy, -dx, dy);
} else {
return false;
}
}
}
bool check(int dx, int dy) {
SX = sx+0.5; SY = sy+0.5;
return go(SX, SY, dx, dy);
}
int main() {
freopen("D-small-attempt0.in", "r", stdin);
freopen("D-small-attempt0.out", "w", stdout);
int ntest;
scanf("%d", &ntest);
for(int test = 1; test <= ntest; test++) {
scanf("%d%d%d", &n, &m, &k);
for(int i=0; i<n; i++) {
scanf("%s", s[i]);
for(int j=0; j<m; j++) {
if(s[i][j] == 'X') {
sx = i;
sy = j;
}
}
}
memset(u, 0, sizeof(u));
for(int dx = -k; dx <= k; dx++) for(int dy = -k; dy <= k; dy++) {
if(dx*dx+dy*dy == 0 || dx*dx+dy*dy > k*k) continue;
if(check(dx, dy)) {
// printf("%d %d\n", dx, dy);
int p = gcd(abs(dx), abs(dy));
u[(dx/p)+50][(dy/p)+50] = true;
}
}
int ans = 0;
for(int dx = -k; dx <= k; dx++) for(int dy = -k; dy <= k; dy++) {
if(u[dx+50][dy+50]) {
// printf("!%d %d\n", dx, dy);
ans++;
}
}
printf("Case #%d: %d\n", test, ans);
}
return 0;
} | 0 |
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 7