can anyone please explain the solution which uses the coordinates i think ... Edited by author 24.01.2023 20:46 Edited by author 24.01.2023 20:46 Edited by author 24.01.2023 20:46 #include<bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while(t--){ string s; cin >> s; int a = int(s[1] - 48); if(s[0]=='a' ||s[0]=='h'){ if(a==1 ||a==8) cout << 2 << endl; else if(a==2||a==7) cout << 3 << endl; else if(a>2) cout << 4 << endl; } else if(s[0]=='b'||s[0]=='g'){ if (a == 1 || a==8) cout << 3 << endl; else if (a == 2|| a==7) cout << 4 << endl; else if (a > 2) cout << 6 << endl; } else{ if (a == 1|| a==8) cout << 4 << endl; else if (a == 2|| a==7) cout << 6<< endl; else if (a > 2) cout << 8<< endl; } } } #include <iostream> #include <vector> #include <iterator> int main() { int N; std::cin >> N; char v; int h, c = 0; std::vector<int> result; while (N--) { std::cin >> v >> h; v = v - 'a' + 1; if (h > 1 && v > 2) c += 1; if (h > 1 && v < 7) c += 1; if (h > 2 && v > 1) c += 1; if (h > 2 && v < 8) c += 1; if (h < 8 && v > 2) c += 1; if (h < 8 && v < 7) c += 1; if (h < 7 && v > 1) c += 1; if (h < 7 && v < 8) c += 1; result.push_back(c); c = 0; } std::copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, "\n")); return 0; } . - empty cage 1 - chess knight 2 - cells under attack 8 | . . . . . . . . 7 | . . . . . . . . 6 | . . 2 . 2 . . . 5 | . 2 . . . 2 . . 4 | . . . 1 . . . . 3 | . 2 . . . 2 . . 2 | . . 2 . 2 . . . 1 | . . . . . . . . ___________________ a b c d e f g h 1 2 3 4 5 6 7 8 WA on test 2 don't know why :( Edited by author 01.07.2016 14:09 Edited by author 01.07.2016 14:09 Edited by author 01.07.2016 14:09 Edited by author 01.07.2016 14:10 i keep having the same problem and i don't know why.Did you find out? It's 2021 now, so... have you found out the problem? :) #include <iostream> using namespace std; int main() { int count; cin >> count; while (count--) { char x, y; cin >> x >> y; float a = (x - 'a') - 3.5; float b = (y - '1') - 3.5; cout << trunc(1.143 * trunc(1.2 * (6 - trunc((a * a + b * b) * 0.2f)))) << endl; } } #include <iostream> using namespace std; int main() { int count; cin >> count; while (count--) { char x, y; cin >> x >> y; float a = abs(abs((x - 'a') - 3.5) - 1.0) - 0.5; float b = abs(abs((y - '1') - 3.5) - 1.0) - 0.5; cout << trunc(1.143 * trunc(1.2 * (6-a-b))) << endl; } } hi how is this issue in java programming language. please help /** * @author koti4 * */ import java.util.*; public class Main { /** * @param args */ public static void main(String[] args) { Scanner input = new Scanner(System.in); int k,n,x,y,flag=0; char x1,y1; String s = " "; n=input.nextInt(); for(int q = 0;q<n;q++) { s=input.next(); x1=s.charAt(0); y1=s.charAt(1); x = (int) x1-96; y = (int) y1-48; /*System.out.println(x); System.out.println(y);*/ for(int i =-2;i<=2;i++) { for(int j=-2;j<=2;j++) {
if (j != i) { if(x-i>0 & y-j>0 & x-i < 9 & y-j < 9) { if(i*i+j*j==5) { flag++; } } } } }
System.out.println(flag); flag = 0; } input.close(); } } #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n--){ string s; int a,b; cin>>s; a = s[0]-'a'+1; b = s[1]-'0'; a = min(a,9-a); b = min(b,9-b); if(a==b){ if(a==1) printf("2\n"); else if(a==2) printf("4\n"); else printf("8\n"); } else { if((a==1&&b==2)||(a==2&&b==1)) printf("3\n"); else if((a==1&&b>=3)||(a>=3&&b==1)) printf("4\n"); else if((a==2&&b>=3)||(a>=3&&b==2)) printf("6\n"); else if(a>2&&b>2)printf("8\n"); } } return 0; } What is wrong with my code? :( #include <stdio.h> #include <stdlib.h> int main (){ int N; scanf ("%d",&N); for (int i = 0 ; i < N ; i++){ char x; int y; getchar(); scanf ("%c %d",&x,&y); int movimientos = 0;
if (y + 2 <= 8) if (x - 1 >= 97){ movimientos++; }
if (y+2 <=8) if (x + 1<=104){ movimientos++; } if (y-2 >= 1) if (x-1 >97){ movimientos++; } if (y-2 >= 1) if (x + 1 <= 104){ movimientos++; }
if (x+2 <= 104) if (y - 1 >= 1){ movimientos++; } if (x+2 <= 104) if (y+1 <= 8){ movimientos++; } if (x-2 >= 97) if (y+1 <= 8){ movimientos++; } if (x-2 >= 97) if (y-1 >= 1){ movimientos++; } printf ("\n %d", movimientos); printf ("\n"); }
return 0; } Edited by author 21.10.2020 01:36 Edited by author 21.10.2020 01:37 just had to consider from 'a' 'h' and '1' '8' #include<bits/stdc++.h> using namespace std; //input section #define si1(x) scanf("%d",&x) //one integer #define si2(x,y) scanf("%d%d",&x,&y) //two integers #define si3(x,y,z) scanf("%d%d%d",&x,&y,&z) //three integers #define sl(x) scanf("%lld",&x) //long long int #define sul(x) scanf("%llu",&x) //unsigned long long #define ss(x) scanf("%s",s) //string #define sd(x) scanf("%lf",&x) //double //output section #define pf printf #define pfi(x) printf("%d\n",x) #define pfl(x) printf("%lld\n",x) #define pfd(x) printf("%lf\n",x) #define nl printf("\n") //data type #define ll long long int #define ld long double #define ull unsigned long long //general #define pb push_back #define mp make_pair #define f first #define s second #define all(x) x.begin( ),x.end( ) #define sv(x) memset(x, 0, sizeof(x)) #define PI 3.1415926535897932384626 #define mod 1000000007 //loop #define lp(a,b) for(int i=a;i<b;i++) //assending order #define lpr(a,b) for(int i=b;i>=a;i--) //decending order #define pl(a) pair<ll, ll>a #define ppi(a) pair<int,int>a #define vec(v) vector<int>v #define vi(v,itr) vector<int>::iterator itr=v.begin( ); void solve( ) { int cnt=0; char s,h; cin>>s>>h; int p,q; p=(int)s-96; q=(int)h-48; if((p+1)>=1 && (p+1)<=8 && (q-2)>=1 && (q-2)<=8) ++cnt; if((p-1)>=1 && (p-1)<=8 && (q+2)>=1 && (q+2)<=8) ++cnt; if((p-1)>=1 && (p-1)<=8 && (q-2)>=1 && (q-2)<=8) ++cnt; if((p+1)>=1 && (p+1)<=8 && (q+2)>=1 && (q+2)<=8) ++cnt; if((p+2)>=1 && (p+2)<=8 && (q-1)>=1 && (q-1)<=8) ++cnt; if((p-2)>=1 && (p-2)<=8 && (q+1)>=1 && (q+1)<=8) ++cnt; if((p-2)>=1 && (p-2)<=8 && (q-1)>=1 && (q-1)<=8) ++cnt; if((p+2)>=1 && (p+2)<=8 && (q+1)>=1 && (q+1)<=8) ++cnt; cout<<cnt<<endl; } int main( ) { //code by noob coder BD (Shahariar,CSE,RUET-19)
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int t=1; cin>>t; while(t--) { solve( ); } return 0; } #include <iostream> #include <stdio.h> #include <vector> using namespace std; class Place { public: char x; int y; bool isOnboard() { if (x < 'a' || x > 'h' || y < 1 || y > 8) return false; return true; } Place(char x, int y) { this->x = x; this->y = y; } }; int main(int argc, const char * argv[]) { int N; cin >> N; vector<Place> places; for (int i = 0; i < N; i++) { char x; int y; cin.get(x); if (x == '\n') { i--; continue; } cin >> y; Place place (x, y); places.push_back(place); } for (int i = 0; i < N; i++) { Place place = places[i]; int moves = 0; //left 2 up 1 place.x -= 2; place.y -= 1; moves += place.isOnboard(); //left 2 down 1 place.y += 2; moves += place.isOnboard(); //right 2 down 1 place.x += 4; moves += place.isOnboard(); //right 2 up 1 place.y -= 2; moves += place.isOnboard(); //right 1 up 2 place.x -= 1; place.y -= 1; moves += place.isOnboard(); //left 1 up 2 place.x -= 2; moves += place.isOnboard(); //left 1 down 2 place.y += 4; moves += place.isOnboard(); //right 1 down 2 place.x += 2; moves += place.isOnboard(); cout << moves << endl; } } Problem solved Edited by author 13.09.2017 11:45 What is the test data for Test#2? package main import ( "bufio" "os" "strconv" "fmt" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) firstLine, _ := reader.ReadString('\n') count, _ := strconv.Atoi(strings.Trim(firstLine, "\n")) result := make([]int, count) for i := 0; i < count; i++ { position, _ := reader.ReadString('\n') result[i] = countFieldsUnderAttack(position) } for _, val := range result { fmt.Println(val) } } func countFieldsUnderAttack(position string) int { result := 0 var positionInts [2]int positionInts[0] = int(position[0]) - int('a') + 1 positionInts[1] = int(position[1]) - int('1') + 1 moves := [8][2]int{ {1, 2}, {2, 1}, {-1, -2}, {-2, -1}, {-1, 2}, {2, -1}, {1, -2}, {-2, 1}, } for _, move := range moves { if move[0] + positionInts[0] > 0 && move[0] + positionInts[0] <= 8 { if move[1] + positionInts[1] > 0 && move[1] + positionInts[1] <= 8 { result++ } } } return result } Edited by author 08.06.2017 23:12 Nevermind. Implemented reading input with bufio scanner and it worked. It seems that i dont understand go reader. #include <iostream> #include <cstdio> #include <cmath> using namespace std; const int MIN = 1, MAX = 8; int free(int x0, int y0) { int c = 0; double r0 = sqrt(5), r; for (int x = x0 - 2; x <= x0 + 2; x++) for (int y = y0 - 2; y <= y0+2; y++) { r = sqrt(pow(x - x0, 2) + pow(y - y0, 2)); if (r == r0 && x >= MIN && x <= MAX && y >= MIN && y <= MAX) c++; }
return c; } int main() { int T; // kolichestvo testovyx blokov cin >> T; cin.ignore();
int x, y; for (int t = 0; t < T; t++) { x = cin.get() - 'a' + 1; y = cin.get() - '0'; cout << free(x, y) << endl; cin.ignore(); } } Edited by author 20.11.2016 00:44 if (x >= MIN && x <= MAX && y >= MIN && y <= MAX) r = sqrt(pow(x - x0, 2) + pow(y - y0, 2)); if (r == r0) c++; Edited by author 20.11.2016 15:06 А в чем суть изменения, которое вы предлагаете? Тот же самый код в итоге, который дает ошибку в первом тесте, как и исходный. You shouldn't compare floats via strict "==". You should better do integer valuations only: int r0 = 25; int r = (x-x0)*(x-x0) + (y-y0)*(y-y0); if (r == r0...) Edited by author 05.03.2017 21:25 Во фрипаскале кейс не принимает стринги, насколько я помню. Попробуйте через ифы: if (a[i] = 'a1') or (a[i] = 'a8') и тд. 'n' is the number of test cases and 'res' it's a function that print the number of positions that can go. The problem is that it only reads one case, for example "a2", prints the result and ends the execution. Why that happen? void res(char p[2]); int main () { int i,n; char pos[2]; scanf("%d", &n); for(i=0; i<n; i++) { scanf("%s", &pos); res(pos); } Edited by author 05.01.2017 08:22 You need at least 3 chars for pos - for ending zero char. Probably it ruined n during first "scanf("%s", &pos);" int a[10]={-1,1,2,2,1,-1,-2,-2,-1,1}; ... for(i=1;i<=8;i++) if(x+a[i-1]>0 && x+a[i-1]<9 && y+a[i+1]>0 && y+a[i+1]<9) h++; DO IT!!! :) #include <stdio.h> #include <iostream> using namespace std; int main() { int n, m, i; char l, dt; cin>>n; for(i=0;i<n;i++){
cin>>l>>dt; if(l=='a' && dt=='1' ||l=='a' && dt=='8' ||l=='h' && dt=='1' ||l=='h' && dt=='8') { m=2; cout<<m<<endl; } if(l=='a' && dt=='2' ||l=='a' && dt=='7' ||l=='b' && dt=='1' ||l=='b' && dt=='8' ||l=='g' && dt=='1'||l=='g' && dt=='8'||l=='h' && dt=='2'||l=='h' && dt=='7') { m=3; cout<<m<<endl; } if(l=='a' && dt=='3' ||l=='a' && dt=='4' ||l=='a' && dt=='5' ||l=='a' && dt=='6' ||l=='b' && dt=='2'||l=='b' && dt=='7'||l=='c' && dt=='1'||l=='d' && dt=='1'||l=='e' && dt=='1'||l=='f' && dt=='1'||l=='c' && dt=='8'||l=='d' && dt=='8'||l=='e' && dt==8||l=='f' && dt=='8'||l=='g' && dt=='7'||l=='g' && dt=='2'||l=='h' && dt=='3'||l=='h' && dt=='4'||l=='h' && dt=='5'||l=='h' && dt=='6') { m=4; cout<<m<<endl; } if(l=='b' && dt=='3' ||l=='b' && dt=='4' ||l=='b' && dt=='5' ||l=='b' && dt=='6' ||l=='c' && dt=='2' ||l=='d' && dt=='2' ||l=='e' && dt=='2' ||l=='f' && dt=='2' ||l=='c' && dt=='7' ||l=='d' && dt=='7'||l=='e' && dt=='7'||l=='f' && dt=='7'||l=='g' && dt=='3'||l=='g' && dt=='4'||l=='g' && dt=='5'||l=='g' && dt=='6') { m=6; cout<<m<<endl; } if(l=='c' && dt=='3' ||l=='c' && dt=='4' ||l=='c'&& dt=='5' ||l=='c' && dt=='6' ||l=='d' && dt=='3' ||l=='d' && dt=='4' ||l=='d' && dt=='5' ||l=='d' && dt=='6' ||l=='e' && dt=='3' ||l=='e' && dt=='4' ||l=='e' && dt=='5' ||l=='e' && dt=='6' ||l=='f' && dt=='3' ||l=='f' && dt=='4' ||l=='f' && dt=='5' ||l=='f' && dt=='6') { m=8; cout<<m<<endl; } } return 0; } I checked all values but something is wrong... That works on my IDE... Sorry, i am noob but this solution must work fine! This is bad, you shouldn't code this way. What if chessboard was 1000x1000 instead of 8x8? Check out this link http://acm.timus.ru/forum/thread.aspx?id=32538&upd=635922147004042008A test_horse function from there is more universal and it can check the amount of moves from any given x and y. You should use something like that instead. Otherwise it's very hard to tell where the error is. this cod is very hard to check. I make the folowing program for(l='a'; l<='h';l++){ for(dt='1';dt<='8';dt++){ ____________________________ this is your cod only replace cout<<m<<endl; with this one cout<<m<<' '; _____________________________ } cout<<endl; } and compare your answer 2,3,4,4,4,4,3,2, 3,4,6,6,6,6,4,3, 4,6,8,8,8,8,6,4, 4,6,8,8,8,8,6,4, 4,6,8,8,8,8,6, 4,6,8,8,8,8,6,4 3,4,6,6,6,6,4,3, 2,3,4,4,4,4,3,2 with the write answer 2,3,4,4,4,4,3,2, 3,4,6,6,6,6,4,3, 4,6,8,8,8,8,6,4, 4,6,8,8,8,8,6,4, 4,6,8,8,8,8,6,4, 4,6,8,8,8,8,6,4, 3,4,6,6,6,6,4,3, 2,3,4,4,4,4,3,2 It is evident that you miss one case e8. I add this case in the branch where m=4,and AC. And even if chessboard was 1000x1000 the best solution is precalc. Good luck.
Edited by author 05.08.2016 23:08 Edited by author 05.08.2016 23:11 Edited by author 05.08.2016 23:14 Edited by author 05.08.2016 23:19 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package test; import java.util.ArrayList; import java.util.Scanner; /** * * @author MMT */ public class P1197 {
public static void main(String[] args){ // declaration of variables Scanner in_number=new Scanner (System.in); Scanner in_string=new Scanner (System.in);
int n=0; ArrayList res=new ArrayList(); String s=" "; char c1=' '; int c2=0; int contr=0; n=in_number.nextInt();
if(n>0 && n<101){ for(int i=0;i<n;i++){
s=in_string.next();
c1=s.charAt(0); c2=Integer.parseInt(s.substring(1)); if((((char)c1+1) <'i') &&(((char)c1+1) >'`') && ((c2+2)<=8) && ((c2+2)>0) ) contr++; if((((char)c1-1) >'`') &&(((char)c1-1) <'i') && ( (c2+2)<=8) && ((c2+2)>0)) contr++; if( (((char)c1+1)<'i') &&(((char)c1+1) >'`') && ( (c2-2)>0) && ( (c2-2)<=8)) // right contr++; if((((char)c1-1) >'`') &&(((char)c1-1) <'i') && ( (c2-2)>0) && ( (c2-2)<=8) ) // right contr++; if((((char)c1+2) <'i') &&(((char)c1+2) >'`') && ( (c2+1)<=8) && ((c2+1)>0))// not checked contr++; if((((char)c1-2) >'`') &&(((char)c1-2) <'i') && ( (c2+1)<=8) && ((c2+1)>0)) // right contr++; if((((char)c1+2) <'i') &&(((char)c1+2) >'`') && ( (c2-1)>0) && ( (c2-1)<=8)) // not checked contr++; if((((char)c1-2) >'`') &&(((char)c1-2) <'i') && ( (c2-1)>0) && ( (c2-1)<=8)) // right contr++;
res.add(contr); contr=0; c1=' '; c2=0; s=" "; } for(int j=0;j<res.size();j++){ System.out.println(res.get(j));
} }/* char x = 'a'; x=(char) (x-1); System.out.println(x); */} } Edited by author 01.07.2016 10:11 |
|