text
stringlengths
49
983k
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <iterator> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) container.begin(), container.end() #define RALL(container) container.rbegin(), container.rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} const int INF = 1<<28; const double EPS = 1e-8; const int MOD = 1000000007; namespace geom{ #define X real() #define Y imag() typedef double R; typedef complex<R> P; struct L : public vector<P>{ L(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);} }; istream& operator>>(istream &is, P &p) { double x, y; is >> x >> y; p = P(x, y); return is; } inline int sig(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline R norm(const P &p){return p.X*p.X+p.Y*p.Y;} inline R inp(const P& a, const P& b){return (conj(a)*b).X;} inline R outp(const P& a, const P& b){return (conj(a)*b).Y;} inline P unit(const P& p){return p/abs(p);} inline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);} inline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);} inline bool sp_intersect(const L &s, const P &p){return !sig(abs(s[0]-p) + abs(s[1] - p) - abs(s[0] - s[1]));} struct G : public vector<P>{ enum {OUT, IN, ON}; P &at(int i){return (*this)[i];} bool contains(const P &p){ R sum = .0; REP(i, size()){ if(sp_intersect(L(at(i), at((i+1)%size())), p)) return ON; // online sum += arg((at(i) - p) / (at((i+1)%size()) - p)); } return sig(sum) ? IN : OUT; } R area(){ R sum = 0; REP(i, size()) sum += outp(at(i), at((i+1)%size())); return abs(sum / 2.); } }; }; using namespace geom; int n; int main(){ ios::sync_with_stdio(false); int T = 0; for(int T = 1;cin >> n, n;T++){ P p; G g; REP(i, n){ cin >> p; g.push_back(p); } printf("%d %.1f\n", T, g.area()); } return 0; }
#include <iostream> #include <string> #include <cmath> #include <cstdio> #include <vector> #include <complex> #include <algorithm> #define rep(i,n) for(int (i)=0;(i)<(int)n;(i)++) #define each(i,o) for (__typeof((o).begin()) i = (o).begin(); i != (o).end(); ++i) #define mp make_pair #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define SORT(x) sort((x).begin(), (x).end()) using namespace std; const double EPS = 1e-8; const double INF = 1e12; class Point{ public: double y,x; Point(){y = x = 0.0;} Point(double iy, double ix){y = iy; x = ix;} Point(double theta){y = sin(theta); x = cos(theta);} Point operator+(const Point& p) const{return Point(y+p.y,x+p.x);} Point operator-(const Point& p) const{return Point(y-p.y,x-p.x);} void operator+=(const Point& p) {y+=p.y;x+=p.x;} void operator-=(const Point& p) {y-=p.y;x-=p.x;} Point operator*(double a) const{return Point(y*a,x*a);} Point operator/(double a) const{return Point(y/a,x/a);} bool operator<(const Point& p) const{return y!=p.y ? y<p.y : x<p.x;} double length() const{return sqrt(x*x+y*y);} double dist(const Point& p) const{return sqrt(pow(y-p.y,2)+pow(x-p.x,2));} double dot(const Point& p) const{return y*p.y+x*p.x;} double cross(const Point& p) const{return y*p.x-x*p.y;} double angle() const{return atan2(y,x);} }; double polyArea(const vector<Point>& vp){ int n = vp.size(); double ret = 0.0; rep(i,n) ret += vp[i].cross(vp[(i+1)%n]); return abs(ret)*0.5; } int main(int argc, char const *argv[]) { int n, count = 0; while(true){ cin >> n; if(n==0) break; vector<Point> vp(n); rep(i,n) cin >> vp[i].x >> vp[i].y; printf("%d %.1lf\n",++count, polyArea(vp)); } return 0; }
#include <iostream> #include <cmath> #include <cstdio> using namespace std; int main() { int n,x[100],y[100],k=1,op[2],oq[2]; while(cin >> n) { if(n==0) break; for(int i=0;i<n;i++) { cin >> x[i] >> y[i]; } double surface=0; for(int i=1;i<n-1;i++) { op[0] = x[i]-x[0]; op[1] = y[i]-y[0]; oq[0] = x[i+1]-x[0]; oq[1] = y[i+1]-y[0]; surface += 0.5 * (op[1]*oq[0] - op[0]*oq[1]); } printf("%d %.1lf\n",k,surface); k++; } }
#include<iostream> #include<iomanip> using namespace std; int main(){ int n, seq = 1; while(cin >> n, n){ int x[n], y[n]; double area = 0; for(int i = 0; i < n; i++) cin >> x[i] >> y[i]; for(int i = 0; i < n; i++){ area += (y[(i+1)%n]*x[i])-(x[(i+1)%n]*y[i]); } cout << seq++ << " " << fixed << setprecision(1) << -area/2.0 << endl; } return 0; }
#include <cstdio> #include <iostream> #include <vector> #include <complex> #include <cmath> #define pb push_back using namespace std; typedef complex<double> P; typedef vector<P> G; /* 外積 */ double cross(const P& a, const P& b) { return imag(conj(a)*b); } /* 多角形の面積 */ #define curr(g, i) g[i] #define next(g, i) g[(i+1)%g.size()] double areaG(const G& g) { double A = 0; for (int i = 0; i < g.size(); ++i) A += cross(curr(g, i), next(g, i)); return abs(A/2.0); } int main(){ G g; double x, y; int n; int cnt = 1; while(cin>>n,n){ cout <<cnt<<" "; for(int i =0; i < n;i++){ scanf("%lf %lf", &x, &y); g.pb(P(x,y)); } printf("%.1f\n",areaG(g)); g.clear(); cnt++; } }
#include<iostream> #include<cmath> #include<iomanip> using namespace std; struct Plane { double x_point; double y_point; }; int main() { int n; int count = 1; Plane *A; while( cin >> n && n != 0) { A = new Plane[n+1]; double area = 0; for(int i=0;i<n;i++) cin >> A[i].x_point >> A[i].y_point; A[n] = A[0]; for(int i=0;i<n;i++) area = area + A[i].x_point * A[i+1].y_point - A[i+1].x_point * A[i].y_point; cout << count << " " << fixed << setprecision(1) << abs(area) / 2 << "\n"; count++; delete [] A; A = NULL; } return 0; }
#include <complex> #include <cmath> #include <iostream> #include <cstdio> using namespace std; typedef complex<double> xy_t; double dot_product(xy_t a, xy_t b) { return (conj(a)*b).real(); } double cross_product(xy_t a, xy_t b) { return (conj(a)*b).imag(); } int main() { int counter = 0; while(true) { counter++; xy_t P[100]; int n; cin >> n; if (n==0) break; for (int i = 0; i < n; i++) { double x, y; cin >> x >> y; P[i] = xy_t(x,y); } //center all coordinates based on first element for (int i = 1; i < n; i++) { P[i] -= P[0]; } //calculate sum double sum = 0.0; for (int i = 1; i+1 < n ; i++) { xy_t a = P[i] , b = P[i + 1]; sum += cross_product(a, b) / 2.0; } printf("%d %.1f\n", counter, abs(sum)); } }
#include<iostream> #include<vector> #include<utility> using namespace std; // computes 2 * area of the triangle p0-p1-p2 template <typename T> T area2(const pair<T,T> p0, const pair<T,T> p1, const pair<T,T> p2) { return (p1.first - p0.first) * (p2.second - p0.second) - (p2.first - p0.first) * (p1.second - p0.second); } // computes 2 * area of the polygon template <typename T> T area2(const vector<pair<T,T>> &ps) { T x = 0; pair<T,T> zero(0,0); int n = ps.size(); for(int i = 0; i < n; i++) { x += area2(zero, ps[i], ps[(i+1)%n]); } return x; } int main(int argc, char *argv[]) { for(int t = 1;; t++) { int n; cin >> n; if(n == 0) break; vector<pair<int,int>> ps; for(int i = 0; i < n; i++) { int x, y; cin >> x >> y; ps.push_back(make_pair(x,y)); } int a2 = abs(area2(ps)); cout << t << " " << (a2/2) << ((a2&1) ? ".5" : ".0") << endl; } return 0; }
/**************** Geometrical Library ****************/ #include<cmath> #include<vector> #include<algorithm> #define EPS 1e-9 #define pb push_back using namespace std; class Point{ public: double x,y; Point(){} Point(double xx,double yy):x(xx),y(yy){} }; class Polygon:public vector<Point>{}; double cross(const Point &a,const Point &b){ return a.x*b.y-a.y*b.x; } double area(const Polygon &pl){ int n=pl.size(); double a=0; for(int i=0;i<n;i++) a+=cross(pl[i],pl[(i+1)%n]); return abs(a)/2; } /**************** Library END ****************/ #include<cstdio> int main(){ for(int n,t=1;scanf("%d",&n),n;t++){ Polygon pl; for(int i=0;i<n;i++){ int x,y; scanf("%d%d",&x,&y); pl.pb(Point(x,y)); } printf("%d %.1f\n",t,area(pl)); } return 0; }
#include <iostream> #include <stdio.h> #include <vector> using namespace std; int main(){ float x,y; int n,count=1,i; float size; cin >> n; while(n!=0){ size=0; vector <float> xs,ys; for(i=0;i<n;i++){ cin >> x >> y; xs.push_back(x); ys.push_back(y); } for(i=0;i<n;i++){ size+=((xs[i]-xs[(i+1)%n])*(ys[i]+ys[(i+1)%n]))/2; } if(size<0) size*=-1; printf("%d %.1f\n",count,size); count++; cin >> n; } return 0; }
#include<iostream> #include<vector> #include<stdio.h> using namespace std; int main(){ int n; int k=1; while(cin>>n,n){ vector<pair<int,int> >v; int x,y; while(n--){ cin>>x>>y; v.push_back(make_pair(x,y)); } double s=0; for(int i=0;i<v.size();i++){ pair<int,int>ne=v[(i+1)%v.size()]; s+=0.5*(ne.first-v[i].first)*(v[i].second+ne.second); } printf("%d %.1f\n",k++,s); } }
#include <iostream> #include <vector> #include <list> #include <stack> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #include <numeric> #include <utility> #define rep(i,n) for(int i = 0;i < n;i++) #define rep2(i,n) for(int i = 1;i <= n;i++) #define each(i,x) for(auto & i : x) using namespace std; int main() { int p = 1; int n; cin >> n; while(n) { vector<int> xa; vector<int> ya; rep(i,n) { int x,y; cin >> x >> y; xa.push_back(x); ya.push_back(y); } double s = 0; rep(i,n) { s += xa[i]*ya[(i+1)%n]-xa[(i+1)%n]*ya[i]; } printf("%d %.1lf\n",p,abs(s)/2.0); cin >> n; p++; } return 0; }
#include <iostream> #include <cmath> #include <cstdio> using namespace std; #define rep(i,a,b) for(int i=(a); i<(b); i++) typedef long double D; int main(){ int n; int cnt=1; while(cin >> n , n){ float sum = 0; int a,b; cin >> a >> b; int c,d; cin >> c >> d; int e,f; rep(i,0,n-2){ cin >> e >> f; int x1 = c-a; int y1 = d-b; int x2 = e-a; int y2 = f-b; sum += x1 * y2 - x2 * y1; c = e; d = f; } printf("%d %.1f\n",cnt++,abs(sum/2)); } return 0; }
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <map> #include <set> #include <string> #include <cstring> #include <cmath> #define PI 3.14159265359 using namespace std; double line(int x1,int x2,int y1,int y2){ //return length of line return sqrt( pow((double)x1-(double)x2,2.0)+ pow((double)y1-(double)y2,2.0)); } //reference:http://imagingsolution.net/math/calc_n_point_area/ //reference:http://d.hatena.ne.jp/nanikaka/20111016/1318734562 int main(int args, char *argc[]){ int n; int count=0; //INPUT while(cin >> n){ count++; double sum=0; if (n==0) return 0; int x[n],y[n]; for(int i=0;i<n;i++){ cin>>x[i]>>y[i]; } for(int i=0;i<n;i++) sum+=((double)x[i]*(double)y[(i+1)%n]-(double)x[(i+1)%n]*(double)y[i])/2; printf("%d %.1f\n",count,-sum); } return 0; } /* //cosA=(b^2+c^2-a^2)/2bc //sinA=cos(90-A) //a(1,2),b(0,1),c(0,2) double a=line(x[1],x[2],y[1],y[2]); double b=line(x[0],x[1],y[0],y[1]); double c=line(x[0],x[2],y[0],y[2]); double alpha=acos((b*b+c*c-a*a)/(2*b*c)); //area=bcsin(Theta) sum+=(sin(alpha))*b*c/2; printf("a:%f,b:%f,c:%f,cos(alpha):%f,sin(alpha):%f,alpha:%f,sum:%f\n", a,b,c,(b*b+c*c-a*a)/2/b/c,sin(alpha),alpha,sum); for(int i=3;i<n;i++){ //a(i-1,i),b(0,i-1),c(0,i) a=line(x[i],x[i-1],y[i],y[i-1]); b=line(x[0],x[i-1],y[0],y[i-1]); c=line(x[0],x[i],y[0],y[i]); alpha=acos((b*b+c*c-a*a)/(2*b*c)); sum+=sin(alpha)*b*c/2; printf("a:%f,b:%f,c:%f,alpha:%f,sum:%f\n", a,b,c,alpha,sum); } printf("%d %.1f\n",count,sum); } return 0; } */
#include<iostream> #include<vector> #include<cmath> #include<iomanip> using namespace std; struct Point { long double px, py; }; long double crs(const Point& a, const Point& b) { return a.px * b.py - a.py * b.px; } long double area(vector<Point> v) { long double ret = 0.0L; for (int i = 0; i < v.size(); i++) ret += crs(v[i], v[(i + 1) % v.size()]); return ret / 2; } int main() { vector<Point>P; Point W; int cnt = 0; while (true) { P.clear(); int n; cnt++; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> W.px >> W.py; P.push_back(W); } cout << cnt << ' ' << fixed << setprecision(1) << fabs(area(P)) << endl; } return 0; }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; inline double prod(int x1, int y1, int x2, int y2) { return x1 * y2 - x2 * y1; } int main() { int N; int count = 1; while (cin >> N) { if (N == 0) break; double area = 0; int x0, y0; cin >> x0 >> y0; int x1, y1; cin >> x1 >> y1; int x2, y2; for (int i = 0; i < N-2; i++) { cin >> x2 >> y2; area += prod(x2-x0, y2-y0, x1-x0, y1-y0); x1 = x2; y1 = y2; } printf("%d %.1f\n", count, fabs(0.5 * area)); count++; } return 0; }
#include<deque> #include<list> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include<algorithm> #include<string> #include<iostream> #include<sstream> #include<cmath> #include<cstdio> #include<cstring> using namespace std; int main(){ int h,i; int n; for(h=1;cin>>n,n;h++){ int x[50],y[50]; for(i=0;i<n;i++) cin>>x[i]>>y[i]; int sm=0; for(i=2;i<n;i++) sm+=(x[i-1]-x[0])*(y[i]-y[0])-(x[i]-x[0])*(y[i-1]-y[0]); printf("%d %.1lf\n",h,fabs(sm)/2); } return 0; }
#include <iostream> #include <vector> #include <cmath> #include <cstdio> using namespace std; struct Point { double x, y; Point(double x_=0, double y_=0):x(x_),y(y_) {} }; Point makePoint(double x, double y) { Point p(x,y); return p; } struct Polygon { vector<Point> pset; Polygon(int size = 3) { pset.assign(size, Point(0, 0)); } }; double areaOfPolygon(const Polygon& polygon) { int n = (int)polygon.pset.size(); double S = 0; for (int i=0; i<n; i++) { S += (polygon.pset[i].x - polygon.pset[(i+1)%n].x)*(polygon.pset[i].y + polygon.pset[(i+1)%n].y); } S = fabs(S); return S/2.; } int main() { int n; int prob = 0; while (1) { cin >> n; if (n==0) break; prob++; Polygon polygon(n); for (int i = 0; i<n; i++) { double x, y; cin >> x >> y; polygon.pset[i] = makePoint(x, y); } double ans = areaOfPolygon(polygon); printf("%d %.1lf\n", prob, ans); string s; getline(cin, s); } return 0; }
#include<iostream> #include<cmath> #include<complex> #include<cstdio> using namespace std; typedef complex<double> P; double cross(P a, P b){ return imag( conj(a) * b );}; P p[64]; int main() { int tno = 0; for(int N; cin >> N, N; ) { for(int i = 0; i < N; i++) { int x, y; cin >> x >> y; p[i] = P(x,y); } double sum = 0; for(int i = 0; i < N; i++) { sum += cross( p[i], p[(i+1) % N]); } printf("%d %.1f\n", ++tno, abs(sum / 2.0)); } }
#include <iostream> #include <cmath> #include <vector> using namespace std; void solve() { int n; int count = 1; while(cin >> n, n) { vector<int> x(n); vector<int> y(n); for(int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; } double Area = 0; for(int i = 1; i < n - 1; ++i) { double x1 = x[i] - x[0]; double y1 = y[i] - y[0]; double x2 = x[i + 1] - x[0]; double y2 = y[i + 1] - y[0]; Area += x1 * y2 - x2 * y1; } printf("%d %.1f\n", count, fabs(Area) / 2); ++count; } } int main() { solve(); return(0); }
#include <stdio.h> int main(){ int n, cnt = 1; scanf("%d", &n); while (n){ float s = 0, x0, y0, xa, xb, ya, yb; scanf("%f %f", &x0, &y0); xb = x0; yb = y0; for (int i = n - 1; i>0; i--){ xa = xb; ya = yb; scanf("%f %f", &xb, &yb); s += (xb + xa)*(yb - ya); } s += (x0 + xb)*(y0 - yb); printf("%d %.1f\n", cnt++, s>0 ? s / 2 : (-s) / 2); scanf("%d", &n); } return 0; }
#include <iostream> #include <cmath> #include <cstdio> #include <cstdlib> #include <complex> using namespace std; int main(){ int n; int cnt=1; while(cin >> n && n){ complex<double> p[n]; double ret = 0; for(int i=0;i<n;i++){ double x; double y; cin >> x >> y; p[i] = complex<double>(x,y); } for(int i=0;i<n;i++){ if(i != n-1){ ret += 0.5*imag(conj(p[i])*p[i+1]); }else{ ret += 0.5*imag(conj(p[i])*p[0]); } } printf("%d %.1lf\n", cnt++, abs(ret)); } }
#include<iostream> #include<cstdio> #include<cmath> #include<complex> #include<vector> #include<queue> using namespace std; typedef complex<int> xy_t; int main() { int n,k = 1; double x,y,sum = 0.0; queue<double> answer; while(cin >> n && n > 0){ vector<xy_t> p(n); for( int i = 0; i < n; ++i){ cin >> x >> y; p[i] = xy_t(x,y); } for( int i = 0; i + 2 < n; ++i){ xy_t a = p[0],b = p[i + 1],c = p[i + 2]; sum += ((conj(b - a)*(c - a)).imag())/2.0; } answer.push(abs(sum)); sum = 0.0; } while(!answer.empty()){ printf("%d %.1f\n",k,answer.front()); answer.pop(); ++k; } }
#include<iostream> #include<cstdio> #include<complex> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) using namespace std; typedef complex<double> point; int main(){ int n,cnt=0; double x,y; while(cin>>n,n){ cnt++; point start,p[n-1]; rep(i,n){ cin>>x>>y; if(i==0)start=point(x,y); else p[i-1]=point(x,y)-start; } double sum=0; rep(i,n-2){ sum+=imag(conj(p[i+1])*p[i])/2; } printf("%d %.1f\n",cnt,abs(sum)); } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) FOR(i,0,n) #define pb push_back #define mp make_pair typedef long long ll; typedef pair<int,int> pint; int main(){ int n,cnt=1; double vx[51],vy[51]; while(cin>>n,n){ rep(i,n) cin>>vx[i]>>vy[i]; int ans=0; rep(i,n) ans += vx[i]*vy[(i+1)%n]-vx[(i+1)%n]*vy[i]; cout<<cnt++<<" "<<fixed<<setprecision(1)<<abs(ans)*0.5<<endl; } return 0; }
#include <cmath> #include <iomanip> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int count = 1; while (true) { int n; cin >> n; if (n == 0) { return 0; } vector<pair<int, int>> v; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; v.emplace_back(x, y); } int s = 0; for (int i = 0; i < n - 1; ++i) { s += v[i].first * v[i + 1].second - v[i + 1].first * v[i].second; } s += v[n - 1].first * v[0].second - v[0].first * v[n - 1].second; cout << count << ' ' << fixed << setprecision(1) << showpoint << abs(s / 2.0) << endl; count += 1; } }
#include <iostream> #include <iomanip> #include <complex> #include <vector> #include <algorithm> #include <cmath> using namespace std; const double EPS = 1e-10; const double INF = 1e12; #define EQ(n,m) (abs((n)-(m)) < EPS) #define X real() #define Y imag() typedef complex<double> P; typedef vector<P> VP; double dot(P a, P b){ return (conj(a)*b).X; } double cross(P a, P b){ return (conj(a)*b).Y; } double getarea(const VP &poly){ double ret = 0; for (int i=0; i<(int)poly.size(); i++){ ret += cross(poly[i], poly[(i+1)%poly.size()]); } return ret*0.5; } int main(){ cout << fixed; cout << setprecision(1); int seq=1; while(1){ int n; cin >> n; if(n==0) break; VP v(n); for(int i=0; i<n; i++){ int x,y; cin >> x >> y; v[i] = P(x,y); } cout << seq++ << " " << abs(getarea(v)) << endl; } return 0; }
#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main(){ int n,count=0; while (cin>>n,n|n!=0){ count++; int x[n]; int y[n]; for (int i=0;i<n;i++){ cin>>x[i]>>y[i]; } double area=0; for(int i=0;i<n;i++){ area=area+(x[i]-x[(i+1)%n])*(y[i]+y[(i+1)%n]); } area=abs((double)(0.5*area)); cout<<count<<" "<<fixed<<setprecision(1)<<area<<endl; } }
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) #define curr(P, i) P[i] #define next(P, i) P[(i+1)%P.size()] typedef complex<double> P; typedef vector<P> G; double cross(const P& a, const P& b) { return imag(conj(a)*b); } double area(const G &P) { double A = 0.0; for (int i = 0; i < P.size(); ++i) A += cross(curr(P, i), next(P, i)); return A / 2.0; } int main() { int t = 1, N; while(cin >>N && N){ G g(N); REP(i, N){ double x, y; cin >>x >>y; g[i] = P(x, y); } printf("%d %.1lf\n", t++, fabs(area(g))); } return 0; }
#include<iostream> #include<complex> #include<vector> #include<cstdio> using namespace std; typedef complex<double> P; typedef vector<P> G; inline double outp(const P & a, const P& b) { return (conj(a)*b).imag(); } double area(G& g) { int n = g.size(); double s=0.0; for(int i=0;i<n;i++) { int j=(i+1)%n; s+=outp(g[i],g[j])/2; } return abs(s); } int main() { int data=1,n; for(;cin>>n,n;data++) { G g; for(int i=0;i<n;i++) { int x,y; cin>>x>>y; g.push_back(P(x,y)); } printf("%d %.1f\n",data,area(g)); // cout<<data<<" "<<area(g)<<endl; } }
#include<iostream> #include<cmath> using namespace std; int main(){ int n; int c=0; while(cin>>n){ if(n==0)break; ++c; double s=0; int x1,xk,y1,yk,xj,yj; cin>>x1>>y1; cin>>xj>>yj; for(int i=0;i<n-2;i++){ cin>>xk>>yk; int px=xj-x1,py=yj-y1,qx=xk-x1,qy=yk-y1; s+=(px*qy-py*qx)/2.0; xj=xk; yj=yk; } printf("%d %.1f\n",c,abs(s)); } }
/*================================================================ * *Vol 11. Area of Polygons *================================================================*/ #include <cstdio> #include <vector> #include <cmath> typedef struct _VERTEX { double x, y; double operator *(_VERTEX &other) { return(x * other.y - y * other.x); } } VERTEX; int main() { std::vector<VERTEX> vertices; int vertexNum; for(int n = 1; ; n ++) { ::scanf("%d", &vertexNum); if(vertexNum == 0) { break; } vertices.resize(vertexNum + 1); for(int i = 0; i < vertexNum; i ++) { VERTEX *lpVert = &vertices[i]; ::scanf("%lf %lf", &lpVert->x, &lpVert->y); } vertices[vertexNum] = vertices[0]; double area = 0.0; for(int i = 0; i < vertexNum; i ++) { area += vertices[i] * vertices[i + 1]; } area = 0.5 * ::fabs(area); ::printf("%d %.1f\n", n, area); } return(0); }
#include <iostream> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <utility> #include <functional> #include <stack> #include <queue> #include <map> #include <set> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <deque> #include <ctime> using namespace std; #define rep(i,n) REP(i,0,n) #define REP(i,s,e) for(int i=(s); i<(int)(e); i++) #define pb push_back #define mp make_pair #define all(r) r.begin(),r.end() #define rall(r) r.rbegin(),r.rend() #define fi first #define se second #define println(X) cout<<X<<endl; #define DBG(X) cout<<#X<<" : "<<X<<endl; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; const ll MOD = 1e9 + 7; double EPS = 1e-8; const double PI = acos(-1); struct Vector2{ double x, y; Vector2(){}; Vector2(double x, double y) : x(x), y(y){}; double cross(const Vector2& p){ return x*p.y - y*p.x; } }; int main(){ int n, c = 0; while(cin>>n && n && ++c){ vector<Vector2> v; rep(i, n){ double x, y; cin>>x>>y; v.pb({x, y}); } double sum = 0.0; v.pb(v[0]); rep(i, n){ sum += v[i].cross(v[i+1]); } printf("%d %.1lf\n", c, -sum/ 2.0); } }
#include <bits/stdc++.h> #define REP(i,n) for(int i=0; i<(int)(n); ++i) using namespace std; typedef complex<double> Point; const double EPS = 1e-8; typedef vector<Point> Polygon; // 反時計回りを仮定 double cross(Point a, Point b){ return imag(conj(a) * b); } Point curr(const Polygon& a, int x){ return a[x]; } Point next(const Polygon& a, int x){ return a[(x + 1) % a.size()]; } double area(const Polygon& P) { double A = 0; for(int i = 0; i < P.size(); i++){ A += cross(curr(P, i), next(P, i)); } return abs(A) / 2.0; } int main(){ int n; int casenum = 0; while(cin >> n && n){ vector<Point> ps(n); REP(i, n){ double x, y; cin >> x >> y; ps[i] = Point(x, y); } printf("%d %.1f\n", ++casenum, area(ps)); } return 0; }
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int N; int n = 1; while (cin >> N, N) { double x[64], y[64]; for (int i=0; i<N; i++) cin >> x[i] >> y[i]; double ans = 0.0; for (int i=0; i<N; i++) ans += x[i]*y[(i+1)%N]-x[(i+1)%N]*y[i]; cout << n++ << ' ' << setprecision(1) << setiosflags(ios::fixed) << -ans/2.0 << endl; } }
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)n; ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; #include <complex> #define CURR(P, i) (P[i]) #define NEXT(P, i) (P[(i + 1) % P.size()]) typedef complex<double> Point; typedef vector<Point> Polygon; struct Line : public vector<Point> { Line() {;} Line(Point a, Point b) { push_back(a); push_back(b); } }; struct Circle { Point p; double r; Circle() {;} Circle(Point p, double r) : p(p), r(r) {;} }; namespace std{ bool operator < (const Point& a, const Point& b){ return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } inline double cross(const Point& a, const Point& b){ return imag(conj(a) * b); } inline double dot(const Point& a, const Point& b){ return real(conj(a) * b); } int ccw(Point a, Point b, Point c){ b -= a; c -= a; double len = abs(b) * abs(c); if(cross(b, c) > +EPS * len) return +1; // counter-clockwise if(cross(b, c) < -EPS * len) return -1; // clockwise if(dot(b, c) < 0) return +2; // c--a--b if(norm(b) < norm(c)) return -2; // a--b--c return 0; // a--c--b } double polygonArea(const Polygon& P) { double A = 0; for (int i = 0; i < P.size(); ++i) A += cross(CURR(P, i), NEXT(P, i)); return abs(A) / 2.0; } int main(){ int n; int cnt = 1; while(cin>>n && n){ printf("%d ", cnt++); Polygon p(n); REP(i, n){ int x, y; cin>>x>>y; p[i] = Point(x, y); } printf("%.1f\n", polygonArea(p)); } return 0; }
#include<bits/stdc++.h> #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3f #define EPS (1e-10) #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef pair<int, int>P; int x[50], y[50]; int main() { int n, cnt = 1; while (scanf("%d", &n), n) { rep(i, n)scanf("%d%d", &x[i], &y[i]); int sum = 0; rep(i, n)sum += x[i] * y[(i + 1) % n] - y[i] * x[(i + 1) % n]; printf("%d %.1lf\n", cnt, abs(sum) / 2.); cnt++; } }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <sstream> #include <cstdio> #include <complex> using namespace std; double cross(complex<double> a, complex<double> b) { return (a * conj(b)).imag(); } int main() { int n, j = 1; while (cin >> n, n) { vector<complex<double> > v(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v[i] = complex<double>(a, b); } double sum = cross(v[0], v[n-1]); for (int i = 1; i < n; i++) { sum += cross(v[i], v[i-1]); } sum = abs(sum) / 2; printf("%d %.1f\n", j, sum); j++; } }
#include <iostream> #include <stdio.h> #include <stdlib.h> #define N 50 using namespace std; int main(){ int fpx, ppx, px, fpy, ppy, py; int n; int sum; int cnt = 1; while(1){ cin >> n; if(n == 0){ break; } sum = 0; cin >> fpx >> fpy; ppx = fpx; ppy = fpy; for(int i = 0; i < n - 1; i ++){ cin >> px >> py; sum += (ppx - px) * (ppy + py); ppx = px; ppy = py; } sum += (ppx - fpx) * (ppy + fpy); printf("%d %.1f\n", cnt, abs(sum) / 2.0); cnt ++; } return 0; }
#include <iostream> #include <vector> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string> using namespace std; int main(){ int n,c=1; while(cin>>n){ if(n==0) break; vector<int> x,y; for(int i=0;i<n;i++){ int xx,yy; cin>>xx>>yy; x.push_back(xx); y.push_back(yy); } double sums=x[x.size()-1]*y[0]-x[0]*y[y.size()-1]; for(int i=0;i<x.size()-1;i++){ sums += x[i]*y[i+1] - x[i+1]*y[i]; } printf("%d %.1lf\n", c++, fabs(sums)/2.0); string str; getline(cin, str); } return 0; }
#include <iostream> #include <string> #include <vector> #include <cstring> #include <climits> #include <algorithm> #include <map> using namespace std; int main() { int n, num = 1; while (cin >> n, n) { vector<int> x(n), y(n); for (int i=0; i<n; ++i) { cin >> x[i] >> y[i]; } double area = 0; for (int i=0; i<n; ++i) { area += (x[(i+1)%n] * y[i] - x[i] * y[(i+1)%n]) / 2.0; } cout.precision(1); cout << fixed << num << " " << abs(area) << endl; num ++; } return 0; }
#include<iostream> #include<cmath> using namespace std; int main() { int n,cnt; double x0,y0,area,x1,y1,x2,y2; cnt=1; while(cin>>n,n){ area=0; cin>>x0>>y0; x1=x0;y1=y0; while(n-->1){ cin>>x2>>y2; area+=x1*y2-x2*y1; x1=x2;y1=y2; } area+=x2*y0-x0*y2; printf("%d %.1f\n",cnt,fabs(area/2.0)); cnt++; } }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main() { int n,s=0,c=0; double a,x[64],y[64]; while(cin >> n, n) { c++; for(int i=0; i<n; i++) { cin >> x[i] >> y[i]; } a=0; for(int i=0; i<n; i++) { a += x[i]*y[(i+1)%n] - x[(i+1)%n]*y[i]; } printf("%d %.1lf\n",c,abs(a)/2); } }
#include <iostream> #include <iomanip> #include <math.h> using namespace std; struct vertex { double x, y; }; double determinant( vertex a, vertex b ) { return ( (a.x * b.y) - (a.y * b.x) ); } int main () { vertex *coordinate; double area; int i, n, order = 0; while( cin >> n ){ if( n == 0 ) break; coordinate = new vertex[n+1]; for( i = 0; i < n; i++ ) cin >> coordinate[i].x >> coordinate[i].y; coordinate[n] = coordinate[0]; area = 0; for( i = 0; i < n; i++) area += determinant( coordinate[i], coordinate[i+1] ); cout << ++order << " " << fixed << setprecision(1) << fabs( area/2 ) << endl; } return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <complex> #include <cmath> #include <algorithm> using namespace std; // * XYツ催?標 #define X real() #define Y imag() // * ツ点ツづ個表ツ個サ typedef complex<double> P; // * ツ仰鳴容ツつキツづゥツ古ォツ債キツε? const double EPS = 1e-10; // * Infinity const double INF = 1e12; // * ツ円ツ偲シツ猟ヲ const double PI = acos(-1.0); // * Infinity 2 const P INF_P(INF,INF); // * complex<double> ツづ個渉?渉伉づーツ津ィツ義ツつキツづゥ namespace std { bool operator < (const P& a, const P& b) { return (real(a) != real(b))? real(a) < real(b) : imag(a) < imag(b); } } // * ツ禿?静?(dot product) : aツ・b = |a||b|cosツδヲ double dot(P a, P b) { return real( conj(a) * b ); } // * ツ外ツ静?(cross product) : aツ×b = |a||b|sinツδヲ double cross(P a, P b) { return imag( conj(a) * b ) ; } int main(){ int n; for(int t=1 ; scanf("%d", &n) , n ; t++ ){ vector<P> v; for(int i=0 ; i < n ; i++ ){ int x, y; scanf("%d %d", &x, &y); v.push_back( P(x,y) ); } double s = 0.0; for(int i=0 ; i < v.size() ; i++ ) { s += 0.5 * (v[(i+1)%v.size()].X - v[i].X) * (v[i].Y + v[(i+1)%v.size()].Y); } printf("%d %.1f\n", t , abs(s) ); } }
#include <cstdio> #include <complex> using namespace std; typedef complex<double> point_t; point_t input[55]; int main(){ int i=1,n; while (scanf("%d",&n)&&n){ for(int k=1;k<=n;k++){ double a,b; scanf("%lf%lf",&a,&b); input[k]=point_t(a,b); } double area=0.; for(int k=0;k<n;k++){ const auto &first=input[k+1],&second=input[(k+1)%n+1]; auto diff=second-first; auto incr=diff.real()*(first.imag()+second.imag()); area+=incr; } printf("%d %.1lf\n",i++,area/2.); } return 0; }
#include <bits/stdc++.h> using namespace std; // http://codeforces.com/contest/280/submission/3275327 typedef complex<double> P; namespace std { bool operator < (const P& a, const P& b) { // if (abs(a-b)<EPS) return 0; return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } double cross(const P& a, const P& b) { return imag(conj(a)*b); } double dot(const P& a, const P& b) { return real(conj(a)*b); } struct L : public vector<P> { L(const P &a, const P &b) { push_back(a); push_back(b); } L() {} }; typedef vector<P> G; #define curr(P, i) P[i] #define next(P, i) P[(i+1)%P.size()] struct C { P p; double r; C(const P &p, double r) : p(p), r(r) { } }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } double area(const G& g) { double A = 0; for (int i = 0; i < g.size(); ++i) { A += cross(g[i], next(g, i)); } return abs(A/2); } int main(){ int t = 1; while(1){ int n; cin >> n; if(n == 0) break; G g; for(int i=0;i<n;i++){ int x, y; scanf("%d%d", &x, &y); g.emplace_back(x, y); } printf("%d %.1f\n", t++, area(g)); } return 0; }
#include <stdio.h> int main() {int N,i,RB[51][2],TT=1;while (1) {scanf("%d",&N);if (!N) return 0;for (i=0;i<N;scanf("%d %d",&RB[i][0],&RB[i][1]),i++);int ans=0;for (i=0;i<N;i++) ans+=RB[i][0]*RB[(i+1)%N][1]-RB[i][1]*RB[(i+1)%N][0];if (ans<0) ans=-ans;printf("%d %.1lf\n",TT++,ans/2.00);}}
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef double D; struct P { D x, y; P(D x_, D y_) : x(x_), y(y_) { } }; D outp(P a, P b) { return a.x*b.y - a.y*b.x; } int main() { int n = 1; while(true) { int N; cin >> N; if(N == 0) break; vector<P> ps; for(int i = 0; i < N; ++i) { D x, y; cin >> x >> y; ps.emplace_back(x, y); } D res = 0.0; for(int i = 0; i < N; ++i) { res -= outp(ps[i], ps[(i+1)%ps.size()]); } res /= 2.0; cout.setf(ios::fixed); cout.precision(1); cout << n << ' ' << res << endl; ++n; } }
#include <stdio.h> #include <math.h> struct vertex{ int x; int y; }; double areaTriangle(vertex p1, vertex p2, vertex p3) { double vec1_x = p2.x - p1.x, vec1_y = p2.y - p1.y; double vec2_x = p3.x - p1.x, vec2_y = p3.y - p1.y; return (vec1_x * vec2_y - vec1_y * vec2_x) / 2; } int main(void) { int cnt = 0; int n; vertex p[50]; double area; while(1){ scanf("%d", &n); if(n == 0) break; for(int i = 0; i < n; i++){ scanf("%d %d", &p[i].x, &p[i].y); } area = 0; for(int i = 1; i+1 < n; i++){ area += areaTriangle(p[0], p[i], p[i+1]); } area = fabs(area); cnt++; printf("%d %.1f\n", cnt, area); } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <cassert> #include <iostream> #include <cctype> #include <sstream> #include <string> #include <list> #include <vector> #include <queue> #include <set> #include <stack> #include <map> #include <utility> #include <numeric> #include <algorithm> #include <bitset> #include <complex> #include <fstream> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for(int i = x; i < n; i++) template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); } template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; } template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; } template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); } //! ツ甘按単ツづ按ポツイツδ督トツクツδ可ス struct P{ double x, y; }; bool operator > (P x, P y) { return x.x > y.x || (x.x == y.x && x.y > y.x); } //! ツ湘」ツ記ツづ個クツδ可スツづ個オツブツジツェツクツト3ツづつづヲツづィツ、ツつサツづェツづーツ陳クツ点ツづつキツづゥツ三ツ角ツ形ツづ個姪環静渉づーツ仰づ淞づゥ double CalcArea(P a, P b, P c) { double A = sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0)); double B = sqrt(pow(b.x - c.x, 2.0) + pow(b.y - c.y, 2.0)); double C = sqrt(pow(c.x - a.x, 2.0) + pow(c.y - a.y, 2.0)); double s = (A + B + C) / 2; return sqrt(s * (s - A) * (s - B) * (s - C)); } int main() { int vn; for(int i = 0; cin >> vn && vn; i++) { vector<P> vs(vn); rep(j, vn) cin >> vs[j].x >> vs[j].y; double res = 0; rep(j, vn-1) { res += (vs[j].x * vs[j+1].y - vs[j].y * vs[j+1].x) / 2; } res += (vs[vn-1].x * vs[0].y - vs[vn-1].y * vs[0].x) / 2; printf("%d %.1f\n", i+1, -res); } }
#include <bits/stdc++.h> using namespace std; using uint=unsigned; using ll=long long; using ull=unsigned long long; #define int ll #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) #define gnr(i,a,b) for(int i=int(b)-1;i>=a;i--) #define per(i,b) gnr(i,0,b) #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back #define fs first #define sc second #define bg begin() #define ed end() #define all(x) x.bg,x.ed #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif #define tmp template<class t> tmp void chmax(t&a,t b){if(a<b)a=b;} tmp void chmin(t&a,t b){if(a>b)a=b;} tmp using vc=vector<t>; tmp using vvc=vc<vc<t>>; using pi=pair<int,int>; using vi=vc<int>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.fs<<","<<p.sc<<"}"; } tmp ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } constexpr ll ten(int n){ return n==0?1:ten(n-1)*10; } tmp void mkuni(vc<t>&v){ sort(all(v)); v.erase(unique(all(v)),v.ed); } ll read(){ll i;cin>>i;return i;} using ld=long double; using cm=complex<ld>; #define x real() #define y imag() const ld eps=1e-12; int sgn(ld a){return a<-eps?-1:(a>eps?1:0);} auto cmcmp=[](const cm&a,const cm&b){ if(sgn(a.x-b.x))return a.x<b.x; else return sgn(a.y-b.y)<0; }; struct ln{ cm a,b; cm dir(){return b-a;} }; ld dot(cm a,cm b){return a.x*b.x+a.y*b.y;} ld crs(cm a,cm b){return a.x*b.y-a.y*b.x;} int ccw(cm a,cm b){return sgn(crs(a,b));} int ccw(cm a,cm b,cm c){return ccw(b-a,c-a);} int ccw(ln a,cm b){return ccw(a.a,a.b,b);} //(-2)[a,-1](0)[b,1](2) int bet(cm a,cm b,cm c){ cm d=b-a; ld e=dot(d,c-a); if(sgn(e)<=0)return sgn(e)-1; return sgn(e-norm(d))+1; } ld tri2(cm a,cm b,cm c){ return crs(b-a,c-a); } ld area2(const vc<cm>&a){ int n=a.size(); ld b=0; rep(i,n) b+=crs(a[i],a[(i+1)%n]); return b; } cm readcm(){ ld a,b; cin>>a>>b; return cm(a,b); } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(1); int t=0; while(1){ int n=read(); if(n==0)break; vc<cm> s(n); rep(i,n)s[i]=readcm(); cout<<++t<<" "<<abs(area2(s)/2)<<endl; } }
#include <cstdio> #include <vector> #include <cmath> using namespace std; int main() { int i, j, n; int **p; vector<double> ans; while (1) { scanf("%d", &n); if (n < 3) break; else p = new int*[n]; for (i = 0; i < n; i++) { p[i] = new int[2]; scanf("%d %d", &p[i][0], &p[i][1]); } double a = 0.0; for (i = 0; i < n; i++) { if (i + 1 >= n) j = 0; else j = i + 1; a += (double)p[i][0] * (double)p[j][1] - (double)p[j][0] * (double)p[i][1]; } ans.push_back(fabs(a / 2)); for (i = 0; i < n; i++) delete[] p[i]; delete[] p; } for (i = 0; i < ans.size(); i++) printf("%d %.1lf\n", i + 1, ans[i]); return 0; }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main() { int n; int j=0; while (cin >> n && n) { j++; double x[n], y[n]; for (int i=0; i<n; i++) cin >> x[i] >> y[i]; double ans = 0; for (int i=1; i<n-1; i++) { double dx1 = x[i]-x[0]; double dy1 = y[i]-y[0]; double dx2 = x[i+1]-x[0]; double dy2 = y[i+1]-y[0]; ans += dy1*dx2 - dx1*dy2; } printf("%d %.1f\n", j, ans/2); } }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main(){ int n; int x[51], y[51]; int count = 1; while(cin >> n && n != 0){ double ans=0; for(int i=0; i<n; ++i){ cin >> x[i] >> y[i]; } for(int i=0; i<n; ++i){ int j=i, k=i+1; if(i==n-1) k=0; ans += x[j]*y[k] - x[k]*y[j]; } ans = fabs(ans)/2; printf("%d %.1f\n", count, ans); count++; } return 0; }
#include <iostream> #include <cmath> #include <iomanip> #include <algorithm> using namespace std; double x[50]; double y[50]; int main(){ int n; int count = 0; while(cin >> n && n != 0){ for(int i = 0; i < n; i++){ cin >> x[i] >> y[i]; } double sum = 0.0; for(int i = 2; i < n; i++){ double dx1 = x[i-1] - x[0]; double dx2 = x[i] - x[0]; double dy1 = y[i-1] - y[0]; double dy2 = y[i] - y[0]; sum += (dx1 * dy2 - dx2 * dy1) / 2; } cout << ++count << " " << fixed << setprecision(1) << abs(sum) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using db = long double; using ll = long long; using vi = vector <int>; #define op operator #define pb push_back struct poi { db x, y; poi op -(poi p) { return {x - p.x, y - p.y}; } db cross(poi p) { return x * p.y - y * p.x; } }p[53]; int main() { cout << fixed << setprecision(1); ios :: sync_with_stdio(0); int _ = 0; for(int n; cin >> n && n; ) { for(int i = 0; i < n; i ++) cin >> p[i].x >> p[i].y; p[n] = p[0]; db ans = 0; for(int i = 0; i < n; i ++) ans += p[i].cross(p[i + 1]); cout << ++ _ << ' ' << -ans / 2 << '\n'; } return 0; }
#include <iostream> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <cmath> #include <cstdio> #include <cstdlib> #include <functional> #include <string> #include <algorithm> #include <string> #include <climits> #define REP(i, n) for (int (i) = 0; (i) < (int)(n); (i)++) #define REG(i, a, b) for (int (i) = ((int)(a)); (i) < ((int)(b)); i++) #define ALL(V) (V).begin(), (V).end() #define PRINT(STR) cout << (STR) << endl typedef long long ll; using namespace std; typedef struct { int x; int y; } Position; double getS(Position p1, Position p2) { double ret = fabs(p1.x * p2.y - p1.y * p2.x); return ret / 2; } int main() { cin.tie(0); //?????±????????????????????? int n; Position pBase; Position pPrev; Position pNow; int count = 0; while (cin >> n, count++, n != 0) { int x, y; cin >> x >> y; Position pNow; Position pPrev { x, y }; const Position pS { x, y }; double sum = 0; for (int i = 1; i < n; i++) { cin >> x >> y; pNow = { x, y }; sum += (pPrev.x - pNow.x) * (pPrev.y + pNow.y); pPrev = { pNow.x, pNow.y }; } sum += (pNow.x - pS.x) * (pNow.y + pS.y); sum = fabs(sum); sum /= 2; printf("%d %.1lf\n", count, sum); } return 0; }
#include <iostream> #include <vector> #include <cmath> #include <cstdio> using namespace std; struct Point2D { double x, y; Point2D(double _x = 0.0, double _y = 0.0) : x(_x), y(_y) { } bool operator==(const Point2D &p) const { return x == p.x && y == p.y; } bool operator!=(const Point2D &p) const { return x != p.x || y != p.y; } bool operator<(const Point2D &p) const { if(x < p.x){ return true; } if(x > p.x){ return false; } return y < p.y; } }; Point2D operator+(const Point2D &a, const Point2D &b){ return Point2D(a.x + b.x, a.y + b.y); } Point2D operator-(const Point2D &a, const Point2D &b){ return Point2D(a.x - b.x, a.y - b.y);} double cross2d(const Point2D &a, const Point2D &b){ return a.x * b.y - a.y * b.x; } double convexArea(const vector<Point2D> &polygon){ double sum = 0.0; for(int i = 1; i < polygon.size(); ++i){ sum += cross2d(polygon[i - 1], polygon[i]); } sum += cross2d(polygon.back(), polygon[0]); return sum / 2; } int main(){ int caseNum = 0; while(true){ int n; cin >> n; if(n == 0){ break; } vector<Point2D> polygon(n); for(int i = 0; i < n; ++i){ cin >> polygon[i].x >> polygon[i].y; } printf("%d %.1lf\n", ++caseNum, abs(convexArea(polygon))); } return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <set> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,j) FOR(i,0,j) #define mp std::make_pair typedef long long ll; typedef unsigned long long ull; typedef std::pair<double,double> P; const int INF = 1001001001; // S N E W(南北東西) const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; int N; P ps[50]; double triangleArea(P p1, P p2, P p3){ P a = mp(p2.first-p1.first, p2.second-p1.second), b = mp(p3.first-p1.first, p3.second-p1.second); return 0.5 * (a.first*b.second - b.first*a.second); } int main(){ int T = 1; while(std::cin >> N, N){ REP(i, N){ std::cin >> ps[i].first >> ps[i].second; } double res = 0.0; for(int i=1;i+1<N;i++){ res += triangleArea(ps[0], ps[i], ps[i+1]); } res = std::abs(res); printf("%d %.1f\n", T, res); T += 1; } }
#include <iostream> #include <iomanip> class Point { public: Point(): x(0), y(0) {} Point(int x, int y): x(x), y(y) {} int x, y; }; double calcTriangle(const Point &first, const Point &second, const Point &third); int cross(const Point &first, const Point &second); double calcArea(Point *array, int start, int end); int calcVectorCross(const Point &first, const Point &second, const Point &third); int main() { int dataNum = 0; while (1) { int pointNum; std::cin >> pointNum; if (pointNum == 0) { break; } dataNum++; Point *pointArray = new Point[pointNum]; for(int i = 0; i < pointNum; i++) { int x, y; std::cin >> x >> y; pointArray[i] = Point(x, y); } std::cout << dataNum << " " << std::fixed << std::setprecision(1) << calcArea(pointArray, 0, pointNum - 1) << std::endl; } return 0; } int calcVectorCross(const Point &first, const Point &second, const Point &third) { Point point1(second.x - first.x, second.y - first.y); Point point2(third.x - first.x, third.y - first.y); return cross(point1, point2); } double calcTriangle(const Point &first, const Point &second, const Point &third) { return calcVectorCross(first, second, third) / 2.0; } int cross(const Point &first, const Point &second) { return first.x * second.y - first.y * second.x; } double calcArea(Point *array, int start, int end) { double area = 0; int first = start + 1; while(!(first == end)) { area += -calcTriangle(array[start], array[first], array[first + 1]); first++; } return area; }
#include <stdio.h> #include <iostream> #include <stack> #include <vector> #include <utility> #include <list> #include <algorithm> #include <cmath> using namespace std; int main() { int count = 1; while (true) { int n; cin >> n; if (n == 0) return 0; int x[n], y[n]; cin >> x[0] >> y[0]; for (int i = 1; i < n; i++) { cin >> x[i] >> y[i]; x[i] -= x[0]; y[i] -= y[0]; } double S = 0; double tmp; for (int i = 2; i < n; i++) { S += x[i] * y[i - 1] - x[i - 1] * y[i]; } S /= 2; printf("%d %.1lf\n", count++, S); } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define repf(i, f, l) for(int i = f; i < (int)l; ++i) #ifdef ONLINE_JUDGE #define DEBUG false #else #define DEBUG true #endif #define pb emplace_back #define lb lower_bound #define ul unsigned long #define ull unsigned long long #define ll long long #define INF 1000000007 #define MOD 1000000007 #define fs first #define sd second #define ALL(c) (c).begin(),(c).end() #define DBG0(x) {if(DEBUG){ cout << #x << ": " << x << "\t"; }} #define DBG(x) {if(DEBUG){DBG0(x); cout << endl;}} #define DBG2(x, y) {if(DEBUG){DBG0(x); DBG(y);}} #define DBG3(x, y, z) {if(DEBUG){DBG0(x); DBG2(y, z);}} #define DBG4(w, x, y, z) {if(DEBUG){DBG0(w); DBG3(x, y, z);}} template <class T> ostream& operator<<(ostream& os, vector<T> xs){ for(T x: xs) os << x << ' '; return os; } template <class S, class T> ostream& operator<<(ostream& os, pair<S,T> st){ os << "(" << st.first << "," << st.second <<")"; return os; } typedef vector<int> vint; typedef vector<ll> vll; typedef vector<ul> vul; typedef vector<ull> vull; typedef vector<bool> vbl; typedef pair<int, int> pii; /* Library starts here */ const double EPS = 1.0e-14; typedef complex<double> Point; /* Line is represented as (a, b, c) of ax + by + c = 0 */ typedef tuple<double, double, double> Line; istream& operator>>(istream& is, Point &pt){ double a, b; is >> a >> b; pt.real(a); pt.imag(b); return is; } ostream& operator<<(ostream& os, const Point &pt){ os << make_pair(pt.real(), pt.imag()); return os; } ostream& operator<<(ostream& os, const Line &l){ os << "(" << get<0>(l) << "," << get<1>(l) << "," << get<2>(l) << ")"; return os; } inline double cross(const Point &a, const Point &b){ return (a.real()*b.imag() - a.imag()*b.real()); } inline double myarg(Point &a){ double ret = arg(a); return (ret >= 0 ? ret : ret + 2*M_PI); } inline double signed_area(const Point &a, const Point &b, const Point &c){ Point ab = b - a; Point ac = c - a; return cross(ab, ac) / 2.0; } inline bool intersect(const Point &a1, const Point &a2, const Point &b1, const Point &b2) { return ((cross(a2-a1, b1-a1) * cross(a2-a1, b2-a1) < EPS) && (cross(b2-b1, a1-b1) * cross(b2-b1, a2-b1) < EPS)); } void sort_by_arg(vector<Point> &pts){ sort(pts.begin(), pts.end(), [](Point a, Point b){ return (myarg(a) < myarg(b)); }); } double area(vector<Point> &vs){ int n = vs.size(); vector<Point> pts(n - 1); rep(i, n - 1) pts[i] = vs[i + 1] - vs[0]; sort_by_arg(pts); double ans = 0.0; rep(i, n - 1) ans += cross(vs[i], vs[i + 1]); return ans / 2.0; } inline Line make_line(const Point &a, const Point &b){ return make_tuple(a.imag() - b.imag() , b.real() - a.real(), -(a.imag() * b.real() - a.real() * b.imag())); // return make_tuple(a.imag() - b.imag() , b.real() - a.real(), a.imag() * b.real() - a.real() * b.imag()); } inline Line orthogonal_line(const Line &l, const Point &p){ return make_tuple(get<1>(l), -get<0>(l), get<0>(l) * p.imag() - get<1>(l) * p.real()); } inline bool is_parallel(const Line &l0, const Line &l1){ return ((get<0>(l0)*get<1>(l1) - get<1>(l0)*get<0>(l1)) == 0.0); } inline Point intersection_point(const Line &l0, const Line &l1){ const double A = get<0>(l0); const double B = get<1>(l0); const double C = get<0>(l1); const double D = get<1>(l1); const double U = get<2>(l0); const double V = get<2>(l1); const double det = A*D - B*C; return Point((-D*U + B*V) / det, ( U*C - A*V) / det); } inline Point rotate(const Point &p, const double theta){ double ct = cos(theta); double st = sin(theta); return Point(ct * p.real() - st*p.imag(), st*p.real() + ct*p.imag()); } /* End of the Library */ const Point O(0,0); int main(void){ int count = 0; while(true){ int n; cin >> n; if(!n) return 0; vector<Point> pts(n); rep(i, n) cin >> pts[i]; double ans = 0.0; rep(i, pts.size() - 1) ans+= signed_area(O, pts[i], pts[i + 1]); ans += signed_area(O, pts.back(), pts[0]); printf("%d %.1lf\n", ++count, abs(ans)); } return 0; }
#include <iostream> #define N 50 using namespace std; int dx[N]; int dy[N]; int main(){ int n; int count=0; while(1){ cin >> n; if(n==0) break; count++; for(int i=0;i<n;i++){ cin >> dx[i] >> dy[i]; } double sum=0; for(int i=0;i<n-1;i++){ sum+=dx[i]*dy[i+1]-dy[i]*dx[i+1]; } sum+=dx[n-1]*dy[0]-dy[n-1]*dx[0]; sum/=2.0; if(sum<0) sum*=-1; printf("%d %.1f\n", count, sum); } return 0; }
/* 座標から面積 a=(x1,y1) b=(x2,y2) とすると S= a*b*sin&#8710;AOB = a*b*√(1-cos^2 &#8710;AOB) = √( (ab)^2 - (a,b)^2 ) =...= √((x1*y2-x2*\ y1)^2) */ #include<iostream> #include<iomanip> #include<cmath> #define rep(i,j) for(i=0;i<j;i++) using namespace std; int main(){ int n,i,cnt=1; while(true){ cin >> n; if(!n)break; int vertices_x[n]; int vertices_y[n]; double area=0; rep(i,n){ cin >> vertices_x[i] >> vertices_y[i]; } rep(i,n-1){ area+=vertices_x[i]*vertices_y[i+1]-vertices_x[i+1]*vertices_y[i]; } area+=vertices_x[n-1]*vertices_y[0]-vertices_x[0]*vertices_y[n-1]; cout << cnt++<<" "<<fixed<<setprecision(1)<<abs(area/2)<<endl; } return 0; }
#include <iostream> #include <cmath> using namespace std; int main() { int n,count; double s; int x[100],y[100]; s=0; count=0; cin>>n; while (n!=0) { for (int i=0;i<n;++i) { cin>>x[i]>>y[i]; } for (int i=0;i<n;++i) { s += (x[i]*y[(i+1)%n]-x[(i+1)%n]*y[i])*1.0; } ++count; printf("%d %.1f\n",count,fabs(s/2.)); s=0; cin>>n; } }
#include <iostream> #include <cmath> #include <cstdio> using namespace std; double area(double a, double b, double c, double d, double e , double f){ double x1 = e - a; double y1 = f - b; double x2 = e - c; double y2 = f - d; return (x1 * y2 - x2 * y1)/2; } int main(void){ int c = 1; int n; while(cin >> n,n){ cout << c++; // inputnum double sum = 0; double bx,by; cin >> bx >> by; double px,py; cin >> px >> py; for(int i=0; i<n-2; i++){ double x,y; cin >> x >> y; sum += area(bx,by,x,y,px,py); px = x; py = y; } printf(" %.1f\n",abs(sum)); } return 0; }
#include<iostream> #include<math.h> #include<iomanip> using namespace std; int main(){ int n; int count=0; cout << fixed << setprecision(1); while(1){ cin >> n; if(n==0)return 0; int x[n],y[n]; for(int i=0;i<n;i++){ cin>>x[i]>>y[i]; } double ans=0; int l; for(int i=0;i<n;i++){ l=(i+1)%n; ans+=(x[i]*y[l]-x[l]*y[i])/2.0; } ans=fabs(ans); count++; cout << count << " " << ans << endl; } }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main(){ int n; double x,y; int cnt = 1; while( cin >> n,n ){ double sum = 0; cin >> x >> y; double oldx = x,oldy = y; double fx = x,fy = y; for(int i = 1 ; i <= n ; i++ ){ if(i == n){ sum+= (oldx - fx)*(oldy + fy); break; } cin >> x >> y; sum += (oldx - x)*(oldy + y); oldx = x; oldy = y; } printf("%d %.1f\n",cnt,fabs(sum/2)); cnt++; } return 0; }
#include <cstdio> #include <cctype> #include <utility> #include <vector> #include <complex> using namespace std; #define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l} #ifdef __linux _gp(_unlocked); #else _gp(); #endif #define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept #define _T template<typename T> #define _HT template<typename H,typename... T> _T _DEF(T,in,int c){T n{};int m{1};while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;} _DEF(int,in,){return in<int>(gcu());} #define _SCAN(...) _DEF(bool,scan,__VA_ARGS__) _T _SCAN(T &n){int c{gcu()};return c==EOF?false:(n=in<T>(c),true);} _DEF(bool, scan, int &c){c=gcu();gcu();return c!=EOF;} #define _OUT(...) _DEF(void,out,__VA_ARGS__) #define _OUTL(...) _DEF(void,outl,__VA_ARGS__) _OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);} #ifdef _GLIBCXX_STRING _SCAN(string &s){int c;s="";for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}} _OUT(string s){for(char c:s)pcu(c);} #endif _T _OUT(T n){static char b[20];char *p{b};T m=n<0?pcu('-'),-1:1;if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);} _OUTL(){out('\n');} #ifdef _GLIBCXX_VECTOR _T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);} #endif _HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);} _HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);} template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();} struct range{int e,b{0},s{1};range(int _b,int _e,int _s):e(_e),b(_b),s(_s){}range(int _b,int _e):e(_e),b(_b){}range(int _e):e(_e){} struct it{int v,s;it(int _v,int _s):v(_v),s(_s){}operator int()const{return v;}operator int&(){return v;}int operator*()const{return v;}it& operator++(){v+=s;return *this;}}; it begin(){return{b, s};}it end(){return{e,s};}}; using P = complex<double>; P read() { int x {in()}; return P(x, in()); } double cross(const P &a, const P &b) { return imag(conj(a) * b); } int main() { int t {1}; for (int n; (n = in());) { vector<P> p(n - 1); P f {read()}; for (auto &i: p) i = read() - f; gcu(); double A {}; for (int i: range(n - 2)) A += cross(p[i], p[i + 1]); printf("%d %.1lf\n", t++, abs(A/2)); } }
#include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> #include<cmath> #include<climits> #include<string> #include<set> #include<map> using namespace std; #define rep(i,n) for(int i=0;i<((int)(n));i++) #define reg(i,a,b) for(int i=((int)(a));i<=((int)(b));i++) #define irep(i,n) for(int i=((int)(n))-1;i>=0;i--) #define ireg(i,a,b) for(int i=((int)(b));i>=((int)(a));i--) typedef long long int lli; typedef pair<int,int> mp; #define fir first #define sec second #define IINF INT_MAX #define LINF LLONG_MAX struct pos{ double x,y; double norm,norm2; pos tov(pos a){ return (a-(*this)); } pos operator+(pos a)const{ pos res=a; res.x+=x; res.y+=y; res=pos(res.x,res.y); return res; } pos operator-(pos a)const{ pos res=(*this); res.x-=a.x; res.y-=a.y; res=pos(res.x,res.y); return res; } pos scalar(double a){ pos res=(*this); res.x*=a; res.y*=a; res=pos(res.x,res.y); return res; } double dot(pos a){ return x*a.x + y*a.y; } double cross(pos a){ return x*a.y - y*a.x; } pos(double ix,double iy){ x=ix; y=iy; norm2=x*x+y*y; norm=sqrt(norm2); } pos(){} string str(){ char ns[50]; sprintf(ns,"(%lf %lf)",x,y); return string(ns); } static pos polar(double r,double t){ return pos(r*cos(t),r*sin(t)); } }; struct polygon{ vector<pos> ps; polygon(){} double area(){ double res=0; rep(i,ps.size()){ pos no=ps[i],to=ps[(i+1)%ps.size()]; res+=(no.x+to.x)*(no.y-to.y); } return abs(res)/2; } }; int main(void){ reg(qq,1,10000){ int n; scanf("%d",&n); if(n==0)break; polygon pl; rep(i,n){ pos p; scanf("%lf%lf",&p.x,&p.y); pl.ps.push_back(p); } printf("%d %.1lf\n",qq,pl.area()); } return 0; }
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1100 #include <iostream> #include <cstdio> #include <iomanip> // setprecision #include <complex> #include <cmath> using namespace std; typedef complex<double> point; double inner_product(point x, point y) { return (conj(x) * y).real(); } double outer_product(point x, point y) { return (conj(x) * y).imag(); } point normalize(point a) { return a * (1/abs(a)); // normはabsの2乗、absは本来のノルム。 } point projection(point a, point b) { point e = normalize(b); return e * inner_product(a, e); } int main() { point P[110]; double x, y; int n; int t = 1; while (cin >> n && n) { for (int i=0; i<n; i++) { cin >> x >> y; P[i] = point(x, y); } double S = 0; for (int i=1; i<n-1; i++) { S += outer_product(P[i]-P[0], P[i+1]-P[0])/2; } cout << t++ << " " << fixed << setprecision(1) << abs(S) << endl; } }
#include <cstdio> #include <cmath> using namespace std; int main() { int n, testcase = 0; while (scanf("%d", &n), n) { int x0, y0, x1, y1, x2, y2; scanf("%d%d%d%d", &x0, &y0, &x1, &y1); x1 -= x0; y1 -= y0; int area = 0; for (int i = 2; i < n; ++i) { scanf("%d%d", &x2, &y2); x2 -= x0; y2 -= y0; area += x1*y2 - x2*y1; x1 = x2; y1 = y2; } printf("%d %.1lf\n", ++testcase, fabs(area / 2.0)); } }
#include <iostream> #include <iomanip> #include <cmath> #include <algorithm> #define rep(i,n) for(int i=0;i<n;++i) using namespace std; struct vert { int x, y; }; vert vs[51]; int main() { int N,num=1; while(cin>>N,N) { rep(i,N) { cin>>vs[i].x>>vs[i].y; } int area = 0; rep(i,N) { int j = (i + 1) % N; area += vs[i].x * vs[j].y - vs[i].y * vs[j].x; } cout.precision(1); cout << num++ << " " << fixed << abs(area) / 2.0 << endl; } return 0; }
#include <iostream> #include <stdio.h> using namespace std; int main(void){ int N = 0; while(1){ N++; int n; cin >> n; if(n == 0){return 0;} double x[n + 1]; double y[n + 1]; for(int i = 0 ; i < n ; i++){ cin >> x[i]; cin >> y[i]; } x[n] = x[0]; y[n] = y[0]; double S = 0; for(int i = 0 ; i < n ; i++){ S -= (x[i] * y[i + 1] - x[i + 1] * y[i]) / 2.0; } printf("%d %.1lf\n", N, S); } }
#include <algorithm> #include <cmath> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <cassert> #include <functional> using namespace std; #define LOG(...) printf(__VA_ARGS__) //#define LOG(...) #define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define REP(i,n) for(int i=0;i<(int)(n);++i) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) #define RSORT(c) sort((c).rbegin(),(c).rend()) #define CLR(a) memset((a), 0 ,sizeof(a)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 }; double Cross(pair<double, double> a, pair<double, double> b){ return a.first*b.second - a.second*b.first; } int main() { int n; int count = 1; while (cin >> n, n){ double ans=0.0; vector<pair<double, double>> pos(n); REP(i,n){ cin >> pos[i].first >> pos[i].second; } REP(i,n){ ans += Cross(pos[i], pos[(i + 1 + n) % n]) / 2.0; } printf("%d %.1f\n", count, -ans); count++; } }
#include <cstdio> #include <cmath> using namespace std; int main(){ int n; for(int i_=1 ; scanf("%d", &n) , n ; ++i_ ){ double x[100],y[100]; for(int i=0 ; i < n ; i++ ){ scanf("%lf %lf", &x[i], &y[i]); } x[n] = x[0]; y[n] = y[0]; double s=0.0; for(int i=0 ; i <= n-1 ; i++ ){ s += ( x[i] - x[i+1] ) * ( y[i] + y[i+1] ); } s = fabs( s/2.0 ); printf("%d %.1f\n", i_ , s ); } }
#include <iostream> #include <iomanip> #include <map> #include <stack> using namespace std; typedef pair<int, int> Pi; #define F first #define S second int crossProduct(Pi p1, Pi p2) { return p1.F * p2.S - p2.F * p1.S; } int main(void) { int n, k = 1; while(cin >> n, n) { stack<Pi> st; int x, y; int sx, sy; cin >> sx >> sy; st.push(make_pair(sx, sy)); for(int i = 1;i < n; i++) { cin >> x >> y; st.push(make_pair(x, y)); } st.push(make_pair(sx, sy)); Pi prev = st.top(); st.pop(); int S = 0; while(st.size()) { Pi p = st.top(); st.pop(); S += crossProduct(prev, p); prev = p; } cout << k++ << ' ' << fixed << setprecision(1) << S / 2.0 << endl; } }
#include <stdio.h> #include <iostream> #include <queue> #include <stack> using namespace std; typedef struct Vector{ int x; int y; } Vector; double outerProduct(Vector v1, Vector v2); int main(){ int pointNum ; queue<double> results; queue<Vector> points; stack<Vector> vecs; int x,y; Vector v1,v2; double area; scanf("%d", &pointNum); while (pointNum != 0) { for (int i = 0; i < pointNum; i++) { cin>>x>>y; points.push({x,y}); } points.push(points.front()); v1 = points.front(); points.pop(); while(!points.empty()){ v2 = points.front(); vecs.push({v2.x-v1.x, v2.y-v1.y}); v1 = v2; points.pop(); } //calculate the area v1 = vecs.top(); vecs.pop(); v2 = vecs.top(); vecs.pop(); area = 0; while (!vecs.empty()) { area += outerProduct(v1, v2); vecs.push({v1.x+v2.x, v1.y+v2.y}); v1 = vecs.top(); vecs.pop(); v2 = vecs.top(); vecs.pop(); } results.push(area); cin>>pointNum; } int i = 1; while (!results.empty()) { area = results.front(); printf("%d %.1f\n", i, area); results.pop(); i++; } return 0; } double outerProduct(Vector v1, Vector v2){ double c = -(v2.x * v1.y - v1.x * v2.y); return c / 2; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define REP(i, n) for((i) = 0; (i) < (n); i++) int base = 1, i, n, x[60], y[60]; double left, right; int main(void) { while(scanf("%d", &n) == 1 && n != 0) { left = right = 0.0; REP(i, n) { scanf("%d%d", &x[i], &y[i]); if(i > 0) { left += x[i - 1] * y[i]; right+= y[i - 1] * x[i]; } } left += x[n-1] * y[0]; right+= y[n-1] * x[0]; printf("%d %.1lf\n", base++, 0.5*fabs(right - left)); } return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <utility> #include <vector> using namespace std; #define X first #define Y second int main() { int n; int cnt=0; while(cin>>n, n) { vector< pair<int,int> > v; for(int i = 0; i < n; i++) { int x,y; cin>>x>>y; v.push_back(make_pair(x,y)); } double ans = 0.0; for(int i = 0; i < n; i++) { ans += (v[i].X-v[(i+1)%n].X)*(v[i].Y+v[(i+1)%n].Y); } ans/=2; ans=abs(ans); printf("%d %.1f\n", ++cnt, ans); } return 0; }
#define _USE_MATH_DEFINES #define INF 100000000 #include <iostream> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> using namespace std; typedef long long ll; typedef pair <double,double> P; typedef pair <int,P> PP; static const double EPS = 1e-8; const int tx[] = {0,1,0,-1}; const int ty[] = {-1,0,1,0}; int main(){ double x,y; int n; int seq=1; while(~scanf("%d",&n)){ if(n==0) break; vector<P> v; for(int i=0;i<n;i++){ scanf("%lf %lf",&x,&y); v.push_back(P(x,y)); } double S=0; for(int i=0;i<v.size();i++){ int idx1 = i; int idx2 = (i+1) % v.size(); S += 0.5*(v[idx2].first - v[idx1].first)*(v[idx1].second + v[idx2].second) + EPS; } printf("%d %.1lf\n",seq++,S); } }
#include <bits/stdc++.h> using namespace std; #define all(c) (c).begin(),(c).end() #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #define iter(c) __typeof((c).begin()) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) #define mem(a) memset(a,0,sizeof(a)) #define pd(a) printf("%.10f\n",a) #define pb(a) push_back(a) #define in(a) insert(a) #define pi M_PI #define R cin>> #define F first #define S second #define C class #define ll long long #define ln cout<<'\n' #define _(_1,_2,_3,N,...)N #define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__) template<C T>void pr1(T a){cout<<a;ln;} template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;} template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;} template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;} bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;} const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; const double EPS = 1e-10; const double INF = 1e12; typedef complex<double> P; namespace std { bool operator < (const P& a, const P& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } double dot(P a, P b){return real(conj(a)*b);} double cross(P a, P b){return imag(conj(a)*b);} int ccw(P a, P b, P c){ b-=a;c-=a; if(cross(b,c)>EPS) return 1; if(cross(b,c)<-EPS) return -1; if(dot(b,c)<-EPS) return 2; if(norm(b)<norm(c)) return -2; return 0; } double area(vector<P> p) { double area=0; for(int i=0; i<(int)p.size(); i++) area+=cross(p[i],p[(i+1)%p.size()]); return -area/2; } void Main() { int n,t=1; while(R n && n) { vector<P> v; rep(i,n) { double x,y; cin >> x >> y; v.pb(P(x,y)); } printf("%d %.1f\n",t++,area(v)); } } int main() { ios::sync_with_stdio(0);cin.tie(0); Main();return 0; }
#include<iostream> #include<vector> #include<cmath> #include<complex> using namespace std; #define REP(i,n) for(int i = 0; i < (int)(n); ++i) typedef double elem;typedef complex<elem> point,vec; elem cross(point a, point b){ return imag(conj(a)*b); }elem AreaOfPolygon(const vector<point> &vp){int n = vp.size();elem ret=0;REP(i,n){ret+=cross(vp[i],vp[(i+1)%n]);}return ret/2.;}int main(){int tc=0;while(true){int n;tc++;vector<point> vp;cin >> n;if(n==0)break;for(int i = 0; i < n; ++i){elem x,y;cin>>x>>y;vp.push_back(point(x,y));}printf("%d %.1lf\n", tc, -AreaOfPolygon(vp) );}return 0;}
#include <string> #include <vector> #include<iostream> #include<cstdio> #include<cstdlib> #include<stack> #include<queue> #include<cmath> #include<algorithm> #include<functional> #include<list> #include<deque> #include<bitset> #include<set> #include<map> #include<cstring> #include<sstream> #include<complex> #include<iomanip> #include<numeric> #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X)) #define rrep(X,Y) for (int (X) = (Y-1);(X) >=0;--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;} template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} #define re real() #define im imag() typedef complex<double> P; typedef pair<P,P> L; //sX+tY const double EPS=1e-10; double dot(P a,P b){return (conj(a)*b).real();} double cros(P a,P b){return (conj(a)*b).imag();} double area(P a,P b,P c){ b-=a; c-=a; return cros(b,c)/2; } double area(vector<P> ps){ double ret=0; rep(i,ps.size()-2) ret+=area(ps[0],ps[i+1],ps[i+2]); return ret; } int main(){ ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(0); int i,j,k,n,t=1; double x,y; while(1){ scanf("%d",&n); if(!n)break; vector<P> ps; rep(i,n){ scanf("%lf %lf",&x,&y); ps.pb(P(x,y)); } printf("%d %.1f\n",t++,abs(area(ps))); } return 0; }
//45 #include<iostream> #include<cmath> #include<iomanip> #include<complex> using namespace std; int main(){ int no=0; for(int n;cin>>n,n;){ typedef complex<double> P; int x,xx,y,yy; cin>>x>>y>>xx>>yy; P p(x,y); P l=P(xx,yy)-p; double area=0; for(int i=0;i<n-2;i++){ int x,y; cin>>x>>y; P ll=P(x,y)-p; area+=-(l.real()*ll.imag()-l.imag()*ll.real())/2; l=ll; } no++; cout<<no<<' '<<fixed<<setprecision(1)<<area<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <bitset> #include <algorithm> #include <numeric> #include <functional> using namespace std; #define Rep(b, e, i) for(int i = b; i <= e; i++) #define Repr(e, b, i) for(int i = e; i >= b; i--) #define rep(n, i) Rep(0, n-1, i) #define repr(n, i) Repr(n-1, 0, i) #define all(v) (v).begin(), (v).end() #define pb(v) push_back(v) #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define bitcnt(x) __builtin_popcount(x) #define fst first #define snd second #define Pqaz(T) priority_queue<T,vector<T>,greater<T>> #define Pqza(T) priority_queue<T> #define put(x) cout << x; #define putsp(x) cout << x << ' '; #define putln(x) cout << x << endl; #define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0); typedef long long ll; typedef pair<ll, ll> llP; typedef pair<int, int> intP; //vector の中身を出力 template <class T>ostream &operator<<(ostream &o,const vector<T>&v) {o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;} void solve(void){ int N, cnt = 1; while (cin >> N && N) { putsp(cnt++); double ans = 0; vector <int> xs(N), ys(N); rep(N, i) { cin >> xs[i] >> ys[i]; } int ox = xs[0], oy = ys[0]; rep(N-2, i) { int dx1 = xs[i+1] - ox, dx2 = xs[i+2] - ox; int dy1 = ys[i+1] - oy, dy2 = ys[i+2] - oy; ans += (dx2*dy1 - dx1*dy2) / 2.; } printf("%.1lf\n", ans); } } int main(void){ solve(); //cout << "yui(*-v・)yui" << endl; return 0; }
#include<stdio.h> int n; int x[99]; int y[99]; int main() { int cnt=0; while(1) { cnt++; int i, j; scanf("%d",&n); if(n==0)break; for(i=0;i<n;i++) scanf("%d %d",&x[i],&y[i]); x[n] = x[0]; y[n] = y[0]; int s = 0; for(i=0;i<n;i++) { s += -x[i]*y[i+1] + x[i+1]*y[i]; } printf("%d %d.%d\n",cnt,s/2,(s%2)*5); } }
#include <cstdio> #include <cstdlib> #include <queue> #define REP(i,n) for(int i=0; i<(int)(n); i++) #include<complex> #define EPS 1e-10 using namespace std; typedef complex<double> P; static inline double outp(const P &a, const P &b){ return (conj(a)*b).imag(); } int main(){ int n; int cc = 1; while(scanf("%d", &n), n){ vector<P> p(n); double ans = 0.0; REP(i,n){ double x, y; scanf("%lf%lf", &x, &y); p[i] = P(x, y); } REP(i,n){ int j = (i + 1) % n; ans += outp(p[i], p[j]) / 2.0; } printf("%d %.1f\n", cc++, std::abs(ans)); } return 0; }
// AOJ 1100 #include<cstdio> #include<cmath> #include<utility> #define rep(i,a) for(int i=0;i<(a);++i) typedef std::pair<int, int> P; int n; P pts[50]; int cross( const P &a, const P &b ) { return a.first*b.second-a.second*b.first; } int main() { int t = 0; while( scanf( "%d", &n ), n ) { rep( i, n ) { int x, y; scanf( "%d%d", &x, &y ); pts[i] = P( x, y ); } double area = 0; rep( i, n ) area += cross( pts[i], pts[(i+1)%n] )/2.0; ++t; printf( "%d %.1f\n", t, fabs(area) ); } return 0; }
#include<cmath> #include<algorithm> #include<iostream> #include<vector> #include<cstdio> #define curr(P, i) P[(i) % P.size()] #define next(P, i) P[(i+1) % P.size()] using namespace std; struct point{ double x, y; }; double areaPol(vector<point> t){ double area=0; for(int i=0;i<t.size();i++) area+=(curr(t,i).x-next(t,i).x)*(curr(t,i).y+next(t,i).y); return abs(area)/2.0; } int main(void){ int n,turn=1; point p; vector<point>t; while(cin >> n,n){ t.clear(); for(int i=0;i<n;i++){ cin >> p.x >> p.y; t.push_back(p); } printf("%d %.1f\n",turn++,areaPol(t)); } return 0; }
#include<iostream> using namespace std; int main(){ int n,x[100],y[100],now=1; while(cin>>n,n){ double ans=0,pre; for(int i=0;i<n;i++) cin>>x[i]>>y[i]; for(int i=1;i<n-1;i++){ pre=((y[i+1]-y[0])*(x[i]-x[0])-(x[i+1]-x[0])*(y[i]-y[0])); ans+=pre; } ans=max(ans,-ans); printf("%d %.1lf\n",now++,ans/2); } return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cstring> #include <map> #include <queue> #include <stack> #include <set> #include <functional> #include <sstream> #include <complex> #include <climits> using namespace std; #define REP(i,a,n) for(int i=(a);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define EPS 1e-8 int main(){ int n; int x[64],y[64]; for(int T=1; scanf("%d",&n),n; T++){ rep(i,n)scanf("%d%d",x+i,y+i); int sum = 0; rep(i,n){ sum += (x[(i+1)%n]-x[i])*(y[(i+1)%n]+y[i]); } printf("%d %.1lf\n",T,(double)sum/2.0); } return 0; }
#include <cstdio> #include <queue> #include <algorithm> #include <map> #include <cstring> using namespace std; struct Board { char state[4][8]; Board(){} }; bool operator< (const Board& lhs, const Board& rhs) { return memcmp(&lhs, &rhs, sizeof(Board)) < 0; } int board[4][8]; void init() { memset(board, 0, sizeof(board)); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 7; ++j) { scanf("%d", board[i]+j+1); } } } int estimateBoard(const Board& x) { int ans = 0; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 8; ++j) if (x.state[i][j] != -1) { if (!((x.state[i][j]>>3) == i && (x.state[i][j]&7) == j)) { ++ans; } } } return ans; } int solve() { map<Board, int> estimate; priority_queue< pair<int, Board> > que; Board start; memset(&start.state, -1, 4*8); for (int i = 0; i < 4; ++i) { for (int j = 1; j < 8; ++j) { if (board[i][j]) { start.state[i][j] = ((board[i][j]/10 - 1)<<3) | (board[i][j]%10 - 1); } if ((start.state[i][j]&7) == 0) { swap(start.state[start.state[i][j]>>3][0], start.state[i][j]); } } } estimate[start] = estimateBoard(start); que.push(make_pair(-estimate[start], start)); for (;!que.empty();) { int dist = -que.top().first; Board cur = que.top().second; que.pop(); int esti = estimate[cur]; if (esti == 0) { return dist; } dist -= esti; for (int i = 0; i < 4; ++i) { for (int j = 1; j < 8; ++j) if (cur.state[i][j] == -1) { char left = cur.state[i][j-1] + 1; for (int ii = 0; ii < 4; ++ii) { for (int jj = 1; jj < 8; ++jj) if (cur.state[ii][jj] == left) { swap(cur.state[ii][jj], cur.state[i][j]); if (estimate.find(cur) == estimate.end()) { estimate[cur] = estimateBoard(cur); int nd = dist + 1 + estimate[cur]; que.push(make_pair(-nd, cur)); } swap(cur.state[ii][jj], cur.state[i][j]); } } } } } return -1; } int main() { int T; scanf("%d", &T); for (int _ = 0; _ < T; ++_) { init(); printf("%d\n", solve()); } return 0; }
// 3:15~ #include <bits/stdc++.h> using namespace std; #define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; const int INF=1e9; const int MOD=1e9+7; const double EPS=1e-9; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& a){ os<<'['; rep(i,a.size()) os<<(i?" ":"")<<a[i]; return os<<']'; } struct Board{ ull data[4]; Board():data{}{} int get(int i,int j){ return data[i]>>j*8&0xff; } void set(int i,int j,int x){ (data[i]&=~(0xffull<<j*8))|=(ull)x<<j*8; } }; bool operator==(const Board& a,const Board& b){ rep(i,4) if(a.data[i]!=b.data[i]) return false; return true; } namespace std{ template<> struct hash<Board>{ size_t operator()(const Board& x)const{ const char* p=(const char*)&x; size_t res=2166136261; rep(i,sizeof(x)) (res^=*p++)*=16777619; return res; } }; } int solve() { Board b0; rep(i,4) repi(j,1,8){ int x; cin>>x; if(x%10==1) b0.set(x/10-1,0,x); else b0.set(i,j,x); } Board bz; rep(i,4) rep(j,7) bz.set(i,j,(i+1)*10+j+1); queue<Board> q; q.push(b0); int res=0; for(;;res++){ queue<Board> q2; unordered_set<Board> vis; while(q.size()){ Board cur=q.front(); q.pop(); if(cur==bz) return res; if(vis.count(cur)) continue; vis.insert(cur); vector<pii> f(48); rep(i,4) rep(j,8) if(cur.get(i,j)) f[cur.get(i,j)]=mp(i,j); rep(i,4) repi(j,1,8) if(cur.get(i,j)==0){ int left=cur.get(i,j-1); if(left==0 || left%10==7) continue; Board next=cur; int x=left+1; next.set(f[x].first,f[x].second,0); next.set(i,j,x); q2.push(next); } } if(q2.empty()) return -1; swap(q,q2); } } int main() { int n; cin>>n; rep(_,n) cout<<solve()<<endl; }
#include<set> #include<queue> #include<cstdio> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; struct board{ int a[4][8]; bool operator<(const board &B)const{ rep(i,4) rep(j,8) if(a[i][j]!=B.a[i][j]) return a[i][j]<B.a[i][j]; return false; } }; bool isgoal(const board &B){ rep(i,4) rep(j,7) if(B.a[i][j]!=(i+1)*10+(j+1)) return false; return true; } int main(){ int T; scanf("%d",&T); while(T--){ board B0; rep(i,4){ B0.a[i][0]=(i+1)*10+1; rep(j,7){ int a; scanf("%d",&a); B0.a[i][j+1]=(a%10==1?-1:a); } } set<board> Vis; // すでに訪れた盤面 Vis.insert(B0); int ans=-1; queue< pair<int,board> > Q; Q.push(make_pair(0,B0)); while(!Q.empty()){ int t=Q.front().first; board B=Q.front().second; Q.pop(); if(isgoal(B)){ ans=t; break; } static int x[48],y[48]; rep(i,4) rep(j,8) if(B.a[i][j]!=-1) { x[B.a[i][j]]=j; y[B.a[i][j]]=i; } rep(i,4) rep(j,8) if(B.a[i][j]==-1) { int left=B.a[i][j-1]; if(left%10<7 && left!=-1){ int tmp=B.a[y[left+1]][x[left+1]]; B.a[i][j]=left+1; B.a[y[left+1]][x[left+1]]=-1; if(Vis.count(B)==0){ Vis.insert(B); Q.push(make_pair(t+1,B)); } B.a[i][j]=-1; B.a[y[left+1]][x[left+1]]=tmp; } } } printf("%d\n",ans); } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <map> using namespace std; typedef pair<int,int> pii; pii numToPlc[101]; int plcToNum[10][10]; int res; map<vector<vector<int> >,int > m; vector<vector<int> > g; bool moveNum(int a){ pii cur=numToPlc[a]; if(a%10==1){ numToPlc[a].first=(a/10)-1; numToPlc[a].second=0; plcToNum[(a/10)-1][0]=a; plcToNum[cur.first][cur.second]=-1; return true; } pii nxtPos=numToPlc[a-1];nxtPos.second++; if(plcToNum[nxtPos.first][nxtPos.second]!=-1 ||nxtPos.second>=8) return false; else{ numToPlc[a]=nxtPos; plcToNum[nxtPos.first][nxtPos.second]=a; plcToNum[cur.first][cur.second]=-1; return true; } } void backNum(int a,pii &p){ pii cur=numToPlc[a]; plcToNum[cur.first][cur.second]=-1; numToPlc[a]=p; plcToNum[p.first][p.second]=a; } vector<vector<int> > trans(){ vector<vector<int> > res(4,vector<int>(8,-1)); for(int i=1;i<=4;i++){ for(int j=1;j<=7;j++){ pii &p=numToPlc[i*10+j]; res[p.first][p.second]=i*10+j; } } return res; } void dfs(int hand){ vector<vector<int> > tmp=trans(); if(g==tmp){ res=min(res,hand); return; } else if(m.find(tmp)!=m.end()&&m[tmp]<=hand)return; m[tmp]=hand; for(int i=1;i<=4;i++){ for(int j=2;j<=7;j++){ pii cur=numToPlc[i*10+j]; bool ok=moveNum(i*10+j); if(ok){ dfs(hand+1); backNum(i*10+j,cur); } } } } int main(){ const int INF=1000000000; for(int i=0;i<4;i++)g.push_back(vector<int>(8,-1)); for(int i=0;i<4;i++) for(int j=0;j<7;j++) g[i][j]=(i+1)*10+(j+1); int t; cin>>t; while(t--){ m.clear(); res=INF; memset(plcToNum,-1,sizeof(plcToNum)); for(int i=0;i<4;i++){ for(int j=0;j<7;j++){ int a; cin>>a; numToPlc[a].first=i; numToPlc[a].second=j+1; plcToNum[i][j+1]=a; } } for(int i=1;i<=4;i++)moveNum(i*10+1); dfs(0); if(res==INF)cout<<-1<<endl; else cout<<res<<endl; } return 0; }
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; } inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int main(){ int N; cin>>N; vector<int> goal(7 * 4); REP(y, 4)REP(x, 7){ int idx = 7 * y + x; if(x + 2 <= 7) goal[idx] = (y + 1) * 10 + x + 2; else goal[idx] = 0; } while(N--){ vector<int> v(7 * 4); REP(i, 7 * 4) cin>>v[i]; REP(i, 7 * 4) if(v[i] % 10 == 1) v[i] = 0; map< vector<int>, int > d; queue< vector<int> > que; que.push(v); d[v] = 0; while(!que.empty()){ vector<int> v = que.front(); que.pop(); if(v == goal) break; int t = d[v]; REP(y, 4)REP(x, 7){ int idx = 7 * y + x; if(v[idx] == 0){ vector<int> v2 = v; int target; if(x == 0) target = (2 + x) + (y + 1) * 10; else target = v[y * 7 + x - 1] + 1; if(target % 10 == 8) continue; v2[idx] = target; REP(idx2, 7 * 4) if(v[idx2] == target){ v2[idx2] = 0; break; } if(!d.count(v2)){ d[v2] = t + 1; que.push(v2); } } } } if(d.count(goal)) cout<<d[goal]<<endl; else cout<<-1<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> Vec; typedef pair<Vec,int> P; bool check(Vec &v){ for(int i = 0 ; i < 32 ; i++){ if((i+1) % 8 == 0) continue; if((i/8+1)*10+i%8+1 != v[i]) return false; } return true; } int bfs(Vec &start,Vec &space){ queue<P> Q; queue<Vec> S; Q.push(P(start,0)); S.push(space); set<Vec> visited; visited.insert(start); while(!Q.empty()){ P p = Q.front(); Q.pop(); Vec s = S.front(); S.pop(); if(check(p.first)) return p.second; for(int i = 0 ; i < 4 ; i++){ if(p.first[s[i]-1] % 10 == 7) continue; for(int j = 0 ; j < 32 ; j++){ if(p.first[s[i]-1]+1 == p.first[j]){ swap(p.first[s[i]],p.first[j]); int tmp = s[i]; s[i] = j; if(!visited.count(p.first)){ visited.insert(p.first); Q.push(P(p.first,p.second+1)); S.push(s); } s[i] = tmp; swap(p.first[s[i]],p.first[j]); } } } } return -1; } int main(){ int Tc; cin >> Tc; while(Tc--){ Vec v(32),space; for(int i = 0 ; i < 32 ; i++){ if(i % 8 != 0){ cin >> v[i]; if(v[i]%10 == 1){ v[i] = 0; space.push_back(i); } }else{ v[i] = (i/8+1)*10+1; } } cout << bfs(v,space) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int bfs(const string &start) { const string goal = "1112131415161700212223242526270031323334353637004142434445464700"; unordered_map<string, int> dist; queue<string> que; dist[start] = 0; que.emplace(start); while(!que.empty()) { const string current = que.front(); que.pop(); if(current == goal) return dist[current]; const int next_dist = dist[current] + 1; for(int i = 2; i < 64; i += 2) { if(current[i] == '0') { string tmp = current.substr(i - 2, 2); ++tmp[1]; int pos = -1; for(int j = 2; j < 64; j += 2) { if(current[j] == tmp[0] && current[j + 1] == tmp[1]) { pos = j; break; } } if(pos == -1) continue; string next(current); next[i] = tmp[0]; next[i + 1] = tmp[1]; next[pos] = '0'; next[pos + 1] = '0'; if(!dist.count(next)) { dist[next] = next_dist; que.emplace(next); } } } } return -1; } void solve() { string start = ""; for(int i = 0; i < 4; ++i) { string tmp = string(1, '1' + i) + "1"; for(int j = 0; j < 7; ++j) { int card; cin >> card; if(card % 10 == 1) { tmp += "00"; } else { tmp += to_string(card); } } start += tmp; } cout << bfs(start) << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); }